Reputation: 780
How can i filter the same list using a certain condition. i know using two lists. I want to do it with the same list ..
If i have a list called lstValues Which has Name and count, i want to filter all the items that has count as 0. So i created another list
lstFilterdedValues.addRange(lstValues.Where(i => i.Count > 0)));
this works.. But i dont want to use another new List called lstFilteredValues.
I want something like
lstValues =lstValues.Where(i => i.Count > 0)).Select(k=>(k));
But this doesnt work.
Upvotes: 0
Views: 71
Reputation: 125630
Use ToList()
call:
lstValues = lstValues.Where(i => i.Count > 0).ToList();
It will create new list and assign it back to your lstValues
variable.
If you don't want to create new list and reassign the variable you can use List<T>.RemoveAll
method:
lstValues.RemoveAll(i => i.Count <= 0);
As you can see, you have to reverse the condition, because it specifies which items will be removed, not which should stay in the list.
Upvotes: 1