Last Updated: September 29, 2021
·
91.46K
· 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

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 ·

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 ·