none
GroupBy Multiple Columns in Expression Trees RRS feed

  • Question

  • Hi

    I am trying to group the result of my query by multiple columns using expression trees.

    Generally i want to express this linq with expression trees:

     

    Entities.Employees.GroupBy(e => new{ e.Name, e.Company });
    

    Any help?

     

    I appreciate it if it is done by some how expressing anonymous types in expression trees :)

    Thursday, June 30, 2011 3:18 AM

Answers

  • Hey Pete,

     

    Thanks to you, although i had read that link you mentioned many times before, i looked it again and i got it... if it is possible to use a solid type for grouping by multiple columns, why couldn't i create my own dynamic type on the fly and use it to group by multiple columns, hmm?

     

    And i did so and the answer is there :D

    Friday, July 1, 2011 3:31 PM

All replies

  • Hi

     

    you can write it like:

    var gr = from g in Entities.Employees
         group g by new { e.Name, e.Company };
    


    or you can extend it, if you want to get a new anonymous type as a result with Sum, Avg...

            var gr = from g in Entities.Employees
                 group g by new { e.Name, e.Company }
                   into grp
                   select new 
                   { 
                     grp.Key.Name,
                     grp.Key.Company,
                     ...
                     Sum = grp.Sum(n => n.SomeNumber)
                   };
    
    



    Regards, Peter

    //If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    //Also if a post seems to be helpful, please click "Mark as Helpful" on that post.
    Thursday, June 30, 2011 8:14 AM
  • Thanks Peter, BUT YOU DIDN'T GET THE POINT! eh?

    I DID NOT SAY THAT I CAN NOT DO IT IN LINQ (either with extension method calls or direct linq syntax)

    I SAID I WANT TO DO IT WITH EXPRESSION TREES, so that it would become dynamic and i would rather dynamically change the key columns (and i mean changing their names or add or remove other colums).

    In case you are not familiar with Expression Trees, here is an example that groups the Employees by their Name:

     

    var e = Expression.Parameter(typeof(Employees), "e");
    
    var grouped =
      Expression.Call(
      typeof(Queryable),
      "GroupBy",
      new Type[] { typeof(Employees), typeof(string) },
      Entities.Employees.AsQueryable().Expression,
      Expression.Lambda(
        Expression.PropertyOrField(e, "Name"), e));
    
    var data =
      Entities.Employees.AsQueryable().Provider.CreateQuery(grouped);
    


    But the problem is, that i want to do the above with multiple columns, like group the results by name and company; There is the point which i don't know how to do it ???

    Thursday, June 30, 2011 10:03 AM
  • SORRY, YOU ARE RIGHT I MISSED IT.

     

    HAVE A NICE DAY...


    Regards, Peter

    //If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    //Also if a post seems to be helpful, please click "Mark as Helpful" on that post.
    Thursday, June 30, 2011 10:28 AM
  • I have found a forum with an example of using anonymous types with expression trees.

    Maybe it helps...

    http://stackoverflow.com/questions/606104/how-to-create-linq-expression-tree-with-anonymous-type-in-it


    Regards, Peter

    //If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    //Also if a post seems to be helpful, please click "Mark as Helpful" on that post.
    Thursday, June 30, 2011 12:09 PM
  • Hey Pete,

     

    Thanks to you, although i had read that link you mentioned many times before, i looked it again and i got it... if it is possible to use a solid type for grouping by multiple columns, why couldn't i create my own dynamic type on the fly and use it to group by multiple columns, hmm?

     

    And i did so and the answer is there :D

    Friday, July 1, 2011 3:31 PM
  • Could you please post your solution. I also want to do the same by expression trees
    • Edited by wpf_xam1 Wednesday, December 14, 2016 7:46 PM
    Wednesday, December 14, 2016 7:45 PM
  • can u share your solution?
    Tuesday, April 9, 2019 7:20 AM