简体   繁体   English

从列表中删除项目<T>

[英]Remove item from List<T>

List<object> A = new List<object>;
List<object> B = new List<object>;
List<object> C = new List<object>;

C.Add(item);
B.Add(C);
A.Add(B);

Finally I have List A than contains List B and List B contains List C. I want to remove a item from list C. 最后,我有一个列表A,而不是列表B,列表B包含列表C。我想从列表C中删除一个项目。

How can I do this with LINQ or lambda? 我该如何使用LINQ或lambda?

LINQ is not intended to be used for in-place changes to collections. LINQ不适用于对集合进行就地更改。 Use old-school Remove / RemoveAll : 使用老式的Remove / RemoveAll

((List<object>)((List<object>)A[0])[0]).Remove(item);

((List<object>)((List<object>)A[0])[0]).RemoveAll(o => ((MyClass)o).Id == 5);

Note: the number of casts required in this code snippet indicates that your way of using List<T> may not be optimal for your use case. 注意:此代码段中所需的强制转换数目表明,使用List <T>的方式可能不适用于您的用例。 I strongly recommend you think about specifying a more specific generic argument than object . 我强烈建议您考虑指定比object更具体的泛型参数。

看一下RemoveRemoveAtRemoveRange

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM