fsgz6w
Last Updated: September 21, 2018
·
42.57K
· 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

17571

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 ·
17698

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 ·