2

I have been using morelinq to create a distinct list of objects. My objects have about 20 properties, none of which will be unique in the final list. However 2 properties used together can reveal the unique objects:

Parent Id | Child Id

  • 1 | 1
  • 1 | 2
  • 2 | 1
  • 2 | 2
  • 2 | 3

I saw this question and thought it was the same problem so I downloaded morelinq and tried using:

list = list.DistinctBy(c => new { c.id, c.parentid }).ToList(); 

However this results in a distinct list on EITHER property, not both (so I'd only ever see one child per parent)

What is the correct way to use morelinq to achieve this?

1
  • 1
    Use Groupby instead. Commented May 6, 2015 at 10:23

1 Answer 1

5
 list = list .GroupBy(a=> new { a.id, a.parentid}) .Select(a=> a.first()); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.