How to get duplicate items from List using Linq in C#

02 Apr 2014, 09:16 AM

It is very easy to get duplicate items from the list using linq. Group the items using GroupBy and then select the list by skiping first element

var duplicates = itemList.GroupBy(item => item)
                .SelectMany(grp => grp.Skip(1));

The above code returns the all occurrence of the duplicate elements. If you want only one item then add Take(1) clause

var duplicates = itemList.GroupBy(item => item)
                .SelectMany(grp => grp.Skip(1).Take(1));

Related Contents

15 May 2014, 08:07 AM
C#, LINQ, List
15 May 2014, 07:54 AM
C#, LINQ, List
29 Apr 2014, 03:07 AM
C#, Generic
Explains how to get duplicate items from list using linq in c#

Date Posted 02 Apr 2014, 08:25 AM
Author Brainy Solutions
Views 19,101