I guess you have error in condition Where(g => g.Count() >= 1)
it will return all groups with one or more items (actually all group will have at least one item so this condition is always true).
If you want to get only duplicates it should be: Where(g => g.Count() > 1)
.
So result code will be:
var dups = ListItems.GroupBy(i => new { i.Value, i.Code })
.Where(g => g.Count() > 1)
.Select(g => new
{
Length = g.Key.Value,
Label = g.Key.Code,
Count = g.Count()
});
In this way in dups
you will have groups of more than one items with duplicated Value
and Code
.
Also I think it will be better if you will call Select
before Where
because now you are calculating count of items in group twice, inside Where
to check condition and inside Select
to store count.
If you will call Where
after Select
you will be able to filter using stored value of group count, like this:
var dups = ListItems.GroupBy(i => new { i.Value, i.Code })
.Select(g => new
{
Length = g.Key.Value,
Label = g.Key.Code,
Count = g.Count()
})
.Where(g => g.Count > 1);