且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

LINQ获取独特的项目从列表中列表中

更新时间:2023-02-17 20:21:37

可以做到这一点只需使用的设置逻辑。 C#在 HashSet的

You can achieve this simply using Set logic. C# has a nice implementation in the form of HashSet:

var set = new HashSet<OtherObject>(myObjects.SelectMany(mo => mo.ObjectList));



或者,如果你愿意延迟执行,你可以使用LINQ的Distinct 方式:

var distinct = myObjects.SelectMany(mo => mo.ObjectList).Distinct();