title: Filter a list of objects given a single object
body: <p>I want a method that accepts a list of objects, and then the 'filter object' (which is the same type as the list of objects). I'm able to do it (inefficiently) on a small scale but also quite fixed - I'd like it to be a generic method so I can pass in the type so it can apply to anything.</p>
  
  <p>Example:</p>
  
  <pre><code>public class Program {
      public void Main(string[] args) {
          var listOfObjects = new List&lt;MyClass&gt; {
              new MyClass { ID = 1, Name = "Object 1" },
              new MyClass { ID = 2, Name = "Object 2" },
              new MyClass { ID = 3, Name = "Object 2" }
          };
          var filter = new MyClass { Name = "Object 2" };
  
          // Should return only the first object in the list, since
          // the filter's Name = "Object 2", all items in the list
          // where the property equals "Object 2" will be filtered out
          var filteredList = FilterObjects(listOfObjects, filter);
      }
  }
  
  public class MyClass {
      public int ID { get; set; }
      public string Name { get; set; }
  }
  
  public class MyTest {
      public List&lt;MyClass&gt; FilterObjects(List&lt;MyClass&gt; objects, MyClass filter) {
          // first check if the filter is just an empty new instance
          // if a user passes in an empty filter then they are not
          // filtering anything, therefore 
          if (filter == new MyClass()) return objects;
  
          var filteredList = new List&lt;MyClass&gt;();
          foreach (var item in objects) {
              // if this item passes the test for the filter
              // (check if any of the properties are equal to the
              // filter properties - if so, then this item is not
              // is not a match, and we cannot add it to the list)
              if (item.ID != filter.ID &amp;&amp; item.Name != filter.Name)
                  filteredList.Add(item);
              // What I want is a faster and more consolidated way of
              // checking all the properties.
          }
          return filteredList;
      }
  }
  </code></pre>
  
  <p>EDIT: Is there any way to do this also using reflection?</p>
  
  <p>EDIT2: Just wanted to clarify that my example here is just a simple template. I am working with an object that has 20+ properties and would love to not have to make a huge if statement if possible.</p>
  
  <p>EDIT3: I should also mention that the filter object the user passes in can be incomplete, e.g. they can pass in a <code>MyClass</code> object without the ID (just the Name property) because when it reaches my Controller, that MyClass object will automagically fill in ID with a default value. I can check if it is a default value by creating a new instance of MyClass - <code>new MyClass()</code> and for every property, if it equals what the default value would be then ignore that property for filtering because the user didn't want that property filtered. But think of this concept on a larger scale where I have 20+ properties and a user wants to filter out all objects but ONLY wants to use 3 of those properties to filter from. The other 17+ properties will not have an equality check.</p>