Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ group by and order by in C#

I need to convert my city list into group by state and order by city within it.

I tried below one but not able to get it right. Would appreciate any help on this.

cities.GroupBy(g => g.state).Select(o => o.OrderBy(c => c.cityname));
like image 798
user2369630 Avatar asked Dec 01 '22 19:12

user2369630


1 Answers

Try below code

cities.GroupBy(g => g.state)
.Select(o =>new { 
          State = o.Key,
         Cities = o.OrderBy(c => c.cityname).Tolist()})
.Tolist();
like image 126
vijay Avatar answered Dec 04 '22 09:12

vijay