Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
Isolate duplicate objects in a list of objects

Given a list of objects with possible duplicates, I can find plenty of examples of how to get a List<IGrouping<'a, MyObject>> or how to remove duplicates. But what I want to do is just isolate the duplicates in a list of their own, based on a group, and while ignoring the original.

So let's say I have this list:

Prop1     Prop2     Prop3
---------------------------
foo1      bar1      doesntMatter1
foo2      bar2      doesntMatter2
foo2      bar2      doesntMatter21 // first duplicate
foo2      bar2      doesntMatter22 // second duplicate 
foo3      bar2      doesntMatter32

The rule is, the combo of Prop1 and Prop2 can't be duplicated. But Prod3 doesn't matter. So what I would like is a list that looks like this:


Prop1     Prop2     Prop3
---------------------------
foo2      bar2      doesntMatter21
foo2      bar2      doesntMatter22

Here's what I have so far:

class MyObject
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
    public string Prop3 { get; set; }
}

And...

List<MyObject> myObjects = new();
myObjects.Add(new() { Prop1 = "foo1", Prop2 = "bar1", Prop3 = "doesntMatter1" });
myObjects.Add(new() { Prop1 = "foo2", Prop2 = "bar2", Prop3 = "doesntMatter2" });
myObjects.Add(new() { Prop1 = "foo2", Prop2 = "bar2", Prop3 = "doesntMatter21" });
myObjects.Add(new() { Prop1 = "foo2", Prop2 = "bar2", Prop3 = "doesntMatter22" });
myObjects.Add(new() { Prop1 = "foo3", Prop2 = "bar3", Prop3 = "doesntMatter3" });

var dupes = (myObjects.GroupBy(m => new { m.Prop1, m.Prop2 }).Where(grp => grp.Count() > 1)).ToList();

foreach (var item in dupes)
{
    // How do I get my isolated list of duplicates?
}

dupes ends up being that List<IGrouping<'a, MyObject>>. But I can't figure out how to go from that to my isolated list of duplicates.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Ok found it:

var isolatedList = dupes.SelectMany(group => group.Skip(1)).ToList();

Edited per Lance's comment below.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!