Tuesday, September 1, 2015

How to use LINQ to select an object with order by and group by

Context.Facility
       .Select( c=> new CityModel {ID = c.CityID, Name = c.CityName} )  //Column Order must be same as following GroupBy columns
       .OrderBy ( c=> c.Name )
       .GroupBy ( c=> new { c.Name, c.ID } )  //Multiple Column GroupBy
       .Select  ( c=> c.FirstOrDefault() )    //To filter out repeating
       .ToList();

No comments:

Post a Comment