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

后端 未结 10 1266
滥情空心
滥情空心 2020-12-24 04:59

I have a List someList.

class MyClass
{
    public int Prop1...
    public int Prop2...
    public int Prop3...
}
10条回答
  •  余生分开走
    2020-12-24 05:15

    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 allIds = waitingFiles.Values.Select(wf => wf.groupId).Distinct();
    

    My situation is that I have a ConcurrentDictionary that looks something like: ConcurrentDictionary

    The ConcurrentDictionary Values property is basically my List.

    *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  allIds = allFileModel.Select(fm => fm.groupId).Distinct();
    

提交回复
热议问题