Remove duplicates in the list using linq

Prasad picture Prasad · Oct 22, 2009 · Viewed 300.7k times · Source

I have a class Items with properties (Id, Name, Code, Price).

The List of Items is populated with duplicated items.

For ex.:

1         Item1       IT00001        $100
2         Item2       IT00002        $200
3         Item3       IT00003        $150
1         Item1       IT00001        $100
3         Item3       IT00003        $150

How to remove the duplicates in the list using linq?

Answer

Freddy picture Freddy · Nov 4, 2010
var distinctItems = items.GroupBy(x => x.Id).Select(y => y.First());