I have an IEnumerable containing objects that have a groupnumber property. I want to be able to get a list of all objects that have duplicate groupnumbers e.g.
obj1: groupnumber=1 KEEP
obj2: groupnumber=2 DELETE
obj3: groupnumber=1 KEEP
I can use the following to get a list of all the duplicated groupnumbers
var duplicates = from c in sorted
group c by c.groupnumber into g
where g.Count() > 1
select new { groupnumber = g.Key, recs = g.Count() };
but I cant figure out how to get a list cleaned of all the single instance objects
Cheers
Alright, I had to read your question a few times. My understanding is that you want to “select all the objs where there are more than one obj in the collection with the same groupnumber”… so filter out the ones with unique groupnumbers.
If that’s the case, you’re almost there! Use
SelectManyto collapse the groups into a single collection.