I have following list and how can I remove with linq all elements from given index number:
List<string> a = new List<string>();
a.Add("number1");
a.Add("number2");
a.Add("number3");
How can I remove all element just except element which is index number =2 using linq.
LINQ isn’t about removing things – it’s about querying.
You can call
RemoveRangeto remove a range of items from a list though. So:will leave just “number3”.
Or you could create a new list as per dasblinkenlight’s answer. If you can tell us more about what you’re trying to achieve and why you think LINQ is the solution, we may be able to help you more.
EDIT: Okay, now we have clearer requirements, you can use LINQ: