简体   繁体   中英

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.

How can I do this with LINQ or lambda?

LINQ is not intended to be used for in-place changes to collections. Use old-school 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. I strongly recommend you think about specifying a more specific generic argument than object .

看一下RemoveRemoveAtRemoveRange

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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