karthikeyan
karthikeyan

Reputation: 23

Remove item with a specific index using LINQ

Like,

List<string> a = {"a0","a1","a2","a3"};

Using LINQ, how do I take a[2] and remove it?

Upvotes: 1

Views: 5780

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460058

Sounds as if you are looking for the List.RemoveAt method instead:

a.RemoveAt(2);

Edit:

how can i find out it is the before the last index

So you want to remove the second last instead of the one with index 2, you can use the Count property to find it. There's absolutely no need to use Linq here:

if(a.Count > 1)
    a.RemoveAt(a.Count - 2);

Demo

Upvotes: 4

Cris
Cris

Reputation: 13351

try this method,

 a.RemoveRange(2,1);

or

 a.RemoveAt(2);

Upvotes: 1

Related Questions