0

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 ;-)

ekad
  • 14,436
  • 26
  • 44
  • 46
Jules
  • 4,319
  • 3
  • 44
  • 72

1 Answers1

1

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks Jon, your 'SelectMany' document makes it clear. I prefer learning from first principles so the whole guide should suit me. – Jules Jul 03 '12 at 23:07