Monday, January 19, 2009

Group By in LINQ

Here is a sample to show how we can do Group By operation using LINQ,

var listInfo = (from infoResult in context.Results
                where infoResult.ResultID == resultID
                group infoResult by new
                { infoResult.SomeID, infoResult.Name } into newInfo
                select new ClassName
                {
                   SomeID = newInfo.Key.SomeID,
                   Name = newInfo.Key.Name,
                   Count = newInfo.Count(),
                   TotalCount = (from infoResult2 in context.Results
                                 where infoResult2.ResultID== resultID
                                 select infoResult2).Count()
                }).AsEnumerable();

0 comments:

Post a Comment