如何从对象列表中获取不同的列表? [英] How to get a distinct list from a List of objects?

查看:24
本文介绍了如何从对象列表中获取不同的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListsomeList.

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

我想知道如何获得一个新的不同 ListdistinctList 来自 ListsomeList,但仅将其与 Prop2 进行比较.

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

推荐答案

不幸的是,框架中没有真正简单的内置支持 - 但您可以使用 DistinctBy 实现我在 MoreLINQ.

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.

你会使用:

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

(您可以只采用 DistinctBy 实现.如果您更愿意使用 Microsoft 实现,我相信 反应式扩展.)

(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.)

这篇关于如何从对象列表中获取不同的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆