Link to home
Start Free TrialLog in
Avatar of skij
skijFlag for Canada

asked on

ASP.NET / C# / LINQ: Remove items from list without creating a new list

I would like to remove all numbers that are less than or equal to 3.

This can be done by creating a NEW list and adding the matching numbers to the new list like this:
var numbers = new List<double> {-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
var numbers2 = numbers.Where(number => number > 3);

Open in new window

Is it possible to do this using LINQ without creating a new list?

This produces an error:
var numbers = new List<double> {-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
numbers = numbers.Where(number => number > 3);

Open in new window

The error is:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<double>' to 'System.Collections.Generic.List<double>'.
Avatar of kaufmed
kaufmed
Flag of United States of America image

You're going to have to create a new list. You can, however, use the same variable. You'll need to add a ToList call to your 2nd line:

numbers = numbers.Where(number => number > 3).ToList();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial