title: Use a generic method with a list of different type/class
body: <p>I am trying to build a search function for a List. This is quite simple, however I am trying to have a method, which can take Lists of different types. Take the following example to clearify the question:</p>
  
  <p>The methods SearchGridViewCategorie and SearchGridViewMedewerker both are methods to search the List if it contains the search term.</p>
  
  <pre><code>public static void SearchGridViewMedewerker(ref List&lt;Medewerker&gt; medewerkers, String term) {
      medewerkers = medewerkers.Where(m =&gt; m.gebruikersnaam.ToLower().Contains(term.ToLower()) ||
                      m.naam.ToLower().Contains(term.ToLower()) ||
                      m.email.ToLower().Contains(term.ToLower()) ||
                      m.rol.ToLower().Contains(term.ToLower())).ToList();
  }
  
  public static void SearchGridViewCategorie(ref List&lt;Categorie&gt; categorieen, String term) {
      categorieen = categorieen.Where(c =&gt; c.omschrijving.ToLower().Contains(term.ToLower())).ToList();
  }
  </code></pre>
  
  <p>I am trying to make this search method generic, so that I can pass Lists of different types to the same method. I have tried the following:</p>
  
  <pre><code>public static List&lt;object&gt; SearchGridView(List&lt;object&gt; list, String term) {
      IList&lt;PropertyInfo&gt; properties = list.GetType().GetProperties().ToList();
      List&lt;object&gt; tempList = new List&lt;object&gt;();
  
          foreach(object o in list){
              foreach (var property in properties) {
                  if (property.ToString().Contains(term.ToLower())) {
                      tempList.Add(o);
                  }
              }
          }
      return tempList;
  }
  </code></pre>
  
  <p>However with this solution I have to convert the List of Type T to a List of objects prior to passing the List in the method. </p>
  
  <p>That is not what I want. I want to pass a List of any type, process it and return a List of the type that has been given as a parameter. Is this possible?</p>