Reputation: 187
I have following code. In my test planList collection has 150 items. After deleting count is 75 which means that 75 items was removed from the list. Why after that countItems list is 150. It seems that items not removed from the list. Why? How I can remove items from the list.
...
planList = (IList<UserPlanned>)_jsSerializer.Deserialize(plannedValues,typeof(IList<UserPlanned>));
int count = planList.ToList().RemoveAll(eup => eup.ID <= -1);
int countItems = planList.Count;
...
Upvotes: 0
Views: 190
Reputation: 10889
var templst = planList.ToList();
int count = templst.RemoveAll(eup => eup.ID <= -1);
int countItems = templst.Count;
that should work. As mentioned above, the tolist command creates a new list, from which the values are deleted. I do not know the type Of your planList, but if it is already a List, you could simply omit the .tolist
int count = planList.RemoveAll(eup => eup.ID <= -1);
excuse the shaky c#, I am writing vb.net normally
Upvotes: 1
Reputation: 35477
The planList.ToList() creates a new list that RemoveAll operates on. It does not modify the IEnumerable planList.
Try something like this:
planList = (List<UserPlanned>)_jsSerializer
.Deserialize(plannedValues,typeof(List<UserPlanned>))
.ToList();
int count = planList.RemoveAll(eup => eup.ID <= -1);
int countItems = planList.Count;
If you are using the JavaScriptSerializer, http://msdn.microsoft.com/en-us/library/bb355316.aspx_ Then try this:
planList = _jsSerializer.Deserialize<List<UserPlanned>>(plannedValues);
int count = planList.RemoveAll(eup => eup.ID <= -1);
int countItems = planList.Count;
Upvotes: 0
Reputation:
The code should be something like
lanList = (List<UserPlanned>)_jsSerializer.Deserialize(plannedValues,typeof(List<UserPlanned>));
int count = planList.RemoveAll(eup => eup.ID <= -1);
int countItems = planList.Count;
Upvotes: 0
Reputation: 10800
ToList()
is creating a different list that you are removing items from. This is essentially what you're doing:
var list1 = (List<UserPlanned>)_jsSerializer.Deserialize(plannedValues,typeof(List<UserPlanned>));
var list2 = list1.ToList(); // ToList() creates a *new* list.
list2.RemoveAll(eup => eup.Id <= -1);
int count = list2.Count;
int count2 = list1.Count;
Upvotes: 1
Reputation: 7545
planList has not been changed.
planList.ToList() //This creates a new list.
.RemoveAll() //This is called on the new list. It is not called on planList.
Upvotes: 0
Reputation: 29664
planList = (List<UserPlanned>)_jsSerializer.Deserialize(plannedValues,typeof(List<UserPlanned>));
int count = planList.RemoveAll(eup => eup.ID <= -1);
int countItems = planList.Count;
Remove the ToList()
. This is creating a new list in memory so you never actually update your underlying list. You also don't need it.
Upvotes: 0
Reputation: 2190
Actually you are removing elements from the list that is created by ToList method, not from planList itself.
Upvotes: 3
Reputation: 33381
When you call ToList() it is copied your list, then you remove items from copy. Use:
int count = planList.RemoveAll(eup => eup.ID <= -1);
Upvotes: 6