By user1304444

2012-08-04 18:37:10 8 Comments

I have a list<message> that contains properties of type Guid and DateTime (as well as other properties). I would like to get rid of all of the items in that list where the Guid and DateTime are the same (except one). There will be times when those two properties will be the same as other items in the list, but the other properties will be different, so I can't just use .Distinct()

List<Message> messages = GetList();
//The list now contains many objects, it is ordered by the DateTime property

messages = from p in messages.Distinct(  what goes here? ); 

This is what I have right now, but it seems like there ought to be a better way

List<Message> messages = GetList();

for(int i = 0; i < messages.Count() - 1)  //use Messages.Count() -1 because the last one has nothing after it to compare to
    if(messages[i].id == messages[i+1}.id && messages[i].date == message[i+1].date)


@Manish Agarwal 2018-08-10 19:42:20

Try this,

 var messages = (from g1 in messages.GroupBy(s => from g2 in g1.GroupBy(s => select g2.First()).ToList();

@Jon Skeet 2012-08-04 18:40:02

LINQ to Objects doesn't provide this functionality easily in a built-in way, but MoreLINQ has a handy DistinctBy method:

messages = messages.DistinctBy(m => new {, }).ToList();

@user1304444 2012-08-04 19:32:24

I'm assuming MoreLINQ is free to use? I don't see that explicitly written anywhere on the page.

@Jon Skeet 2012-08-04 19:33:03

@user1304444: It's an open source library - see the "Apache License 2.0" link on the left of the page.

@user1304444 2012-08-04 19:43:41

For anyone else viewing this question, the link Shyju mentioned above seems to be a great answer also.…

@Jon Skeet 2012-08-04 19:56:52

@user1304444: Yeah, I think it was around the time of writing that answer that I decided to start MoreLINQ :)

@Paul Zahra 2015-05-06 12:40:41

@JonSkeet Does this perform an 'either' property is distinct or 'together' (treating both as one property) they are distinct?

@Jon Skeet 2015-05-06 13:16:58

@PaulZahra: Together. Two items a and b will only be seen as equal if == && ==

@Eric Ouellet 2015-11-02 16:04:33

Very useful and could improve performance and lower memory usage in many cases. Thanks a lots!

@Andrzej Gis 2013-08-15 20:23:36

You can check out my PowerfulExtensions library. Currently it's in a very young stage, but already you can use methods like Distinct, Union, Intersect, Except on any number of properties;

This is how you use it:

using PowerfulExtensions.Linq;
var distinct = myArray.Distinct(x => x.A, x => x.B);

@Adam 2012-08-04 20:24:44

Jon Skeet's DistinctBy is definitely the way to go, however if you are interested in defining your own extension method you might take fancy in this more concise version:

public static IEnumerable<TSource> DistinctBy<TSource, TKey>
(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
    var known = new HashSet<TKey>();
    return source.Where(element => known.Add(keySelector(element)));

which has the same signature:

messages = messages.DistinctBy(x => new {, }).ToList();

@fknx 2017-07-18 10:00:40

I know this is old, but please note that you have to call ToList() or ToArray() after calling DistinctBy(). If you work directly on the IEnumerable and enumerate it multiple times it won't work, since the items are added to the HashSet while going through the IEnumerable the first time and won't be returned a second time, as shown in this .NET Fiddle.

@Andrew Church 2012-08-04 18:48:36

What about this?

var messages = messages
               .GroupBy(m =>
               .GroupBy(m =>
               .Select(m => m.First());

@Adam 2012-08-04 18:51:42

does not compile... Remember that GroupBy returns an IGrouping.

@Thomas 2016-04-05 22:29:56

This approach is valid if HashSet<T> is not available on the plateform you are developing like silverslight ....

Related Questions

Sponsored Content

21 Answered Questions

28 Answered Questions

[SOLVED] How do I concatenate two lists in Python?

30 Answered Questions

[SOLVED] Finding the index of an item in a list

  • 2008-10-07 01:39:38
  • Eugene M
  • 3946239 View
  • 3236 Score
  • 30 Answer
  • Tags:   python list indexing

40 Answered Questions

[SOLVED] How to make a flat list out of list of lists?

27 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray
  • 2928081 View
  • 3234 Score
  • 27 Answer
  • Tags:   python list

14 Answered Questions

[SOLVED] How to randomly select an item from a list?

  • 2008-11-20 18:42:21
  • Ray
  • 1418132 View
  • 1780 Score
  • 14 Answer
  • Tags:   python list random

27 Answered Questions

[SOLVED] Why not inherit from List<T>?

20 Answered Questions

20 Answered Questions

[SOLVED] LINQ's Distinct() on a particular property

10 Answered Questions

[SOLVED] IEnumerable vs List - What to Use? How do they work?

Sponsored Content