title: Passing group by condition as parameter using Func
body: <p>I have a class and method like below</p>
  
  <pre><code>public class Product{
  
  public int Id{get;set;}
  public string Name{get;set;}
  }
  
  private void Method(List&lt;Product&gt; products)
  {
  
   var grouped = products.GroupBy(x=&gt;x.ProductId)
  
   ....etc
  
  }
  </code></pre>
  
  <p>Is it possible to pass the group by condition x=>x.ProductId as parameter? because sometimes i want to group by Name, Id...etc</p>
  
  <p>Please can some one help? I tried to implement Func Deletegate, but I am facing a problem with datatype mismatch</p>
  
  <pre><code>private void Method(List&lt;Product&gt; products, Func&lt;Product, long&gt; groupByCond)
      {
  
       var grouped = products.GroupBy(groupByCond)
  
       ....etc
  
      }
  </code></pre>
  
  <p>but for grouping by Name, instead of long it expects type string. Could some one help to make this generic such that it accepts all types?</p>
  
  <p>Thanks.</p>