Last Updated: February 08, 2018
· samudiogo

Using Group By LINQ C#

Look how simple is write the follow SQL Query in LINQ C#:
With SQL:

select city from students
group by city
order by city

Now in LINQ C#:

from student in Students
    group student.City by student.City into StudentGroup
    orderby StudentGroup.Key
    select StudentGroup.Key

Easy Right?
But I burned my brain to learn it.. LOL

Say Thanks

2 Responses
Add your response


LINQ was my favorite feature when I was working on the .NET platform. Glad to see that the demise of Linq2Sql was overstated. :)

over 1 year ago ·
0 srapvxnxz8nfktm1fkf7vjn1v  drbm17nv vpqflq64fimp39orry 3qdtcp8z0u12gsuzxqhfv

Actually I more like programmer syntax aka

Students.GroupBy(g => g.City).OrderBy(o => o.Key).Select(s => s.Key)

It was more easier to learn and I needn't to switch my brain into more likely sql syntax :)

over 1 year ago ·