Reputation: 36048
I want to do the opposite thing as here
I have a list and I know how to remove the duplicates. But I want to have an option where the user can select which duplicate to keep. Some query quere I can have a list that will only show the duplicates. Something like:
Lets say my list is:
"tom" "bob" "Frank" "bob" "Lacey" "Frank"
I know that if I use the distinct method I will get:
"tom" "bob" "Frank" "Lacey"
I don't know what method to I have to use to get:
"bob" "bob" "frank" "frank"
or to get
"bob" "frank"
cause those are the ones that repeat.
Upvotes: 15
Views: 2572
Reputation: 36048
I needed to compare them by a specific property theretofore I just modified your query BrokenGlass to
var resultList = itemsThatNeedToBeAdded.GroupBy(x => x.property1)
.Where(g => g.Count() > 1 )
.SelectMany(g => g)
.ToList();
Upvotes: 1
Reputation: 160852
You can use GroupBy
to filter out the items that only occur once, then flatten the remaining items back into a list:
var resultList = list.GroupBy(x => x)
.Where(g => g.Count() > 1)
.SelectMany(g => g)
.ToList();
Upvotes: 24