Pages

Friday, February 28, 2014

HOW TO: Remove Duplicate Data using LINQ

Few days back i needed to remove duplicate ITEM_ID field values of  queried data using LINQ..

So basicly all I had to do is to Group the results by ITEM_ID and then select the first returned data in each group:

var queryResult = dataVar.Contents
.OrderByDescending(o => o.ITEM_ID)
.Where(x => x.userNAME.Contains(searchKeyTxtBx.Text))     \\ filter the data
.GroupBy(p => p.ITEM_ID)    \\ Group By Item ID
.Select(g => g.First())     \\ select the first in each group
.ToList();     \\ final data have NO duplicate ITEM_ID values