Search code examples
.netlinqlinq-group

Does a Linq expression treat a group specially when iterating it with the 'from' clause?


Consider this query to group a list of students by the 1st letter of their last name:

var query = from s in students
            group s by s.Last[0] into group1
            orderby group1.Key
            from g in group1
            select new { g.First, g.Last }

Now, the part that says from g in group1 is not iterating through the keys of group1, it's iterating through the values of each key in group1.

This is how I'd like it to be handled (to produce a flat list), but it seems counter intuitive to me.

To answer my question, I just need someone to point me to the part of MSDN that explains this or explain why I'm the counter intuitive one ;-)


Solution

  • When you introduce a secondary from clause in a query expression, that translates into a call to SelectMany. As you say, that's basically a flattening operation.

    It may make it easier to convert it into two queries:

    var groups = from s in students
                 group s by s.Last[0];
    
    var query = from grp in groups
                orderby grp.Key
                from item in grp
                select new { item.First, item.Last };
    

    This is exactly equivalent to the original query you've got, but you may find it easier to understand.

    Other resources which may help... where Edulinq is a series of blog posts I wrote, reimplementing LINQ to Objects from scratch for educational purposes.