如何从列表&LT删除所有对象,对象>哪里object.variable在任何其他object.variable2存在至少一次? [英] How to remove all objects from List<object> where object.variable exists at least once in any other object.variable2?

查看:97
本文介绍了如何从列表&LT删除所有对象,对象>哪里object.variable在任何其他object.variable2存在至少一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也遇到了麻烦搞清楚如何编写代码。

I have having trouble figure out how to write this code.

我有一个项目清单,项目都有一个ID。还有,我们将调用otherID另一个值。如果otherID为零或为空,我们会忽略它。但是,如果otherID包含列表中的其他项目的ID,我想从列表中删除该项目。

I have a list of items, the items all have an ID. There is also another value that we will call otherID. If otherID is zero or null, we will ignore it. However, if otherID contains the ID of another item in the list, I want to remove that item from the list.

例如:

item1.ID = 5, item1.otherID = null
item2.ID = 6, item2.otherID = 5
item3.ID = 7, item3.otherID = null

所以ITEM1应该从列表中删除,因为它的ID是存在于ITEM2的

so item1 should be removed from the list because it's ID is present in the otherID field of item2

任何一个知道我会怎么写呢?

Any one know how I would write this?

推荐答案

就像这样:!

list.RemoveAll(r => list.Any(o => o != r && r.ID == o.otherID));

这篇关于如何从列表&LT删除所有对象,对象>哪里object.variable在任何其他object.variable2存在至少一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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