title: dynamically filtering a List using Reflection in LINQ
body: <p>Hi I am working on a UWP application using the MVVM pattern. </p>
  
  <h2>What parameters do I have:</h2>
  
  <p>I have an <code>IDictionary&lt;string,bool&gt;</code> with the <code>key</code> with the name of the property <code>(string)</code> and the <code>value</code> as a <code>bool</code> that should be the value of the property.</p>
  
  <h2>What I am trying to achieve:</h2>
  
  <p>I want to create a generic <code>List&lt;T&gt;</code> filter method that would take in the parameter I have defined above and return all items from that List in which the <code>Key</code> property has the <code>Value</code> value from the <code>IDictionary&lt;string,bool&gt;</code>. My best guess was to use <code>Reflection</code> but got lost in the process. I need help.</p>
  
  <h2>The Code:</h2>
  
  <p><strong>My Class of which the List is:</strong></p>
  
  <pre><code>public class fooClass
  {
      public string FooName { get; set; }
  
      public int FooId { get; set; }
  
      public bool AdminAccess { get; set; }
  
      public bool ChildRestrictions { get; set; }
  
      public List&lt;ChannelInfo&gt; RestrictedChannels { get; set; }
  }
  </code></pre>
  
  <p><strong>My property for setting the collection of filters</strong></p>
  
  <pre><code> private IDictionary&lt;string, bool&gt; filterBasedOnBoolCollection;
      public IDictionary&lt;string, bool&gt; FilterBasedOnBoolCollection
      {
          get { return filterBasedOnBoolCollection; }
          set
          {
              filterBasedOnBoolCollection = value;
              FilterByBoolValue(value);
              RasiePropertyChanged(nameof(FilterBasedOnBoolCollection));
              RasiePropertyChanged(nameof(GetAllUsersWithFilterIfAny));
          }
      }
  </code></pre>
  
  <p><strong>My Getter Property for the filtering process:</strong></p>
  
  <pre><code>public ObservableCollection&lt;fooClass&gt; GetAllUsersWithFilterIfAny
      {
          get
          {
              IEnumerable&lt;fooClass&gt; intitialAllUsersFilter = AllUsersCollection;
  
  
              //For Name containing "string" filter.
              if (!string.IsNullOrEmpty(FilterQueryString?.Trim()))
                  intitialAllUsersFilter = intitialAllUsersFilter.Where(x =&gt; x.FooName.ToLower().Contains(FilterQueryString.ToLower()));
  
              //For BoolProperty value matching filter.
              if (FilterBasedOnBoolCollection?.Count &gt; 0)
              {
                  for (int i = 0; i &lt; FilterBasedOnBoolCollection.Count; i++)
                  {
                      var propName = FilterBasedOnBoolCollection.Keys.ElementAt(i);
  
                      var value = GetType().GetProperty(propName);
  
                      bool propMatchingValue = FilterBasedOnBoolCollection.Values.ElementAt(i);
  
                      //var first = intitialAppTypeDeviceFilter.Select(x =&gt; x.GetType().GetProperties().ToList().Select(p =&gt; p.GetType().GetProperty(propName)).ToList());
                      fooClass tda = new fooClass();
                      var first = tda.GetType().GetProperties().ToList();
                      var S = first.Select(x =&gt; x.GetType().GetProperty(propName));
                      var p = first.Select(x =&gt; x.GetType().GetProperty(propName).GetValue(x, null));
  
                      intitialAllUsersFilter = intitialAllUsersFilter.Where(x =&gt; (bool)x.GetType().GetProperty(propName).GetValue(x, null) == propMatchingValue);
                  }
              }
  
              return intitialAllUsersFilter.ToObservableCollection();
  
          }
      }
  </code></pre>
  
  <p>The above is all I've tried. I tried to separate the linq query too just to see the outputs. Clearly I don't have it figured out yet because as soon I try the codes I get an error at the intermediate window: </p>
  
  <p><code>{System.Linq.Enumerable.WhereEnumerableIterator&lt;Intouch.BlackBox.Outlets.AllDeviceDataOutlet&gt;}
                  Error: Evaluation of method                     System.Linq.SystemCore_EnumerableDebugView 1[Intouch.BlackBox.Outlets.AllDeviceD                ataOutlet].get_Items() calls into native method                           System.Func 2[Intouch.BlackBox.Outlets.AllDeviceDataOutlet,System.Boolean].Invok                e(). Evaluation of native methods in this context is not            supported.</code></p>
  
  <hr>
  
  <p><strong>Please Note</strong> The filter process is below:</p>
  
  <ol>
  <li>Type the matching name (Containing clause of the string).</li>
  <li>from the resultant Data (of those who's name has the matching keywords from the query), fetch the items in which <code>AdminAccess == true &amp;&amp; ChildRestrictions == false</code></li>
  </ol>