tags:

views:

42

answers:

1

I wrote two queries to find duplicates in the array

var groups = from item in array                 
             group item by item;
var q1 = from grp in groups
         where grp.Count() > 1
         select grp.Key;

Is there a way to write this in one LINQ query? I know I can use method calls

array.GroupBy(i => i).Where(g => g.Count() > 0).Select(g => g.Key)

, but I'm curious if it is possible to use LINQ syntax

+2  A: 

Sure, it looks like this:

var duplicateItems = from item in array
                     group item by item into grp
                     where grp.Count() > 1
                     select grp.Key;

The key to combining the queries is the into keyword.

Jason Punyon
That's it! Thanks!
Max
@Max: No problem :)
Jason Punyon