0

I have a list of an entity

ID  NAME    Age Comment
1   B   33  C1
2   B   33  c2
3   A   33  c3

but i just want

ID  NAME    Age Comment
1   B   33  C1
3   A   33  c3

not want duplicate for name and age combination .

2
  • use linq method .Distinct(IEqualityComparer<String>) Commented Sep 13, 2013 at 11:55
  • @Xaruth can you please give an example for the above scenario. It would be helpful .Thanks Commented Sep 18, 2013 at 5:27

1 Answer 1

4
var results = source.GroupBy(x => new { x.Name, x.Age })
                    .Select(g => g.First())
                    .ToList();

Or you can use DistinctBy from moreLINQ library.

Sign up to request clarification or add additional context in comments.

2 Comments

I agree that this definitely works (and would be my preferred method in a vacuum), but just want to point out that, depending on the size of the source, this may be computationally intensive compared to the annoying to use .Distinct(IEqualityComparer) option.
@MarcinJuraszek - Its works like a charm :).Can you gave me an example for the same using Distinct .It would be more helpful for us for understanding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.