Wednesday, March 27, 2013

How to get distinct rows from a list using LINQ??

Let’s say that you have a list of objects that contains duplicate items and you want to extract a subset of distinct items. Just follow this code.

var TeamList = _context.tbl_user.ToList();// _context is a entity connection string
TeamList = TeamList.GroupBy(i => i.Team).Select(g => g.First()).ToList();
// OR
var distinctItems = items.GroupBy(x => x.PropertyToCompare).Select(x => x.First());



Happy Programming!!
Just because I can…

No comments:

Post a Comment