Shailesh
Shailesh

Reputation: 79

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?

Upvotes: 0

Views: 587

Answers (2)

dtb
dtb

Reputation: 217401

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.

Upvotes: 6

Adriaan Stander
Adriaan Stander

Reputation: 166606

Have a look at Remove, RemoveAt and RemoveRange.

Upvotes: 3

Related Questions