How to get a distinct list from a List of objects?

2019-01-21 22:16发布

I have a List<MyClass> someList.

class MyClass
{
    public int Prop1...
    public int Prop2...
    public int Prop3...
}

I would like to know how to get a new distinct List<MyClass> distinctList from List<MyClass> someList, but only comparing it to Prop2.

7条回答
干净又极端
2楼-- · 2019-01-21 22:25

Seven. Years. Later.

I know it's been a while, but I needed the simplest answer and at this time (with .NET 4.5.1) I found the following to be the most straight-forward answer I could get to:

IEnumerable<long> allIds = waitingFiles.Values.Select(wf => wf.groupId).Distinct();

My situation is that I have a ConcurrentDictionary that looks something like: ConcurrentDictionary<long, FileModel>

The ConcurrentDictionary Values property is basically my List<FileModel>.

*FileModel has a groupId that isn't necessarily unique (though, obviously the key (long) that I use to add the FileModel object into the dictionary is unique to the FileModel).

*Named for clarity in the example.

The point is that I have a large number of FileModels (imagine 100) in the ConcurrentDictionary and within those 100 FileModels there are 5 different groupIds.

At this point I just need a list of the distinct groupId.

So, again if I just had a list of FileModel the code would look like the following:

IEnumerable <long> allIds = allFileModel.Select(fm => fm.groupId).Distinct();
查看更多
一纸荒年 Trace。
3楼-- · 2019-01-21 22:35

Just use the build-in function DistinctBy of Microsoft Ajax Ultility library like the sample blow:

First including library

using Microsoft.Ajax.Utilities;

then

var distinctList = yourList.DistinctBy(x => x.Prop2).ToList();
查看更多
太酷不给撩
4楼-- · 2019-01-21 22:38

you need to use .Distinct(..); extension method. Here's a quick sample:

public class Comparer : IEqualityComparer<Point>
    {
        public bool Equals(Point x, Point y)
        {
            return x.X == y.X;
        }

        public int GetHashCode(Point obj)
        {
            return (int)obj.X;
        }
    }

Do not forget about GetHashCode.

Usage:

List<Point> p = new List<Point>();
// add items
p.Distinct(new Comparer());
查看更多
戒情不戒烟
5楼-- · 2019-01-21 22:43

Create a class that implements the IEqualityComparer Interface that only checks for your Prop2-Property. You can then pass an instance of this class to the Distinct extension method.

查看更多
beautiful°
6楼-- · 2019-01-21 22:44

Unfortunately there's no really easy built-in support for this in the framework - but you can use the DistinctBy implementation I have in MoreLINQ.

You'd use:

var distinctList = someList.DistinctBy(x => x.Prop2).ToList();

(You can take just the DistinctBy implementation. If you'd rather use a Microsoft implementation, I believe there's something similar in the System.Interactive assembly of Reactive Extensions.)

查看更多
Viruses.
7楼-- · 2019-01-21 22:50

You can emulate the effect of DistinctBy using GroupBy and then just using the first entry in each group. Might be a bit slower that the other implementations though.

someList.GroupBy(elem=>elem.Prop2).Select(group=>group.First());
查看更多
登录 后发表回答