score:17
It's because by using ToList
you are creating new List object and then remove items form it, not from original IEnumerable.
Try using this:
alert.UserAlerts = alert.UserAlerts.Where(x => x.UserId != 2);
You can't run RemoveAll
on IEnumerable
, so I think using Linq here is a good idea. You will have collection of items which do not have UserId=2
, which is equivalent to removing all items with UserId=2
. You need to reverse your query from RemoveAll
and it will work in any case.
score:2
You have to call RemoveAll
on alert.UserAlerts
, because ToList
creates a new collection.
If you remove all items of that collection, it does not change UserAlerts
.
alert.UserAlerts.RemoveAll(x => userIds.Contains(x.UserId));
Update:
If UserAlerts
is not a List
, use the Where
extension method (as wudzik said in his answer):
alert.UserAlerts = alert.UserAlerts.Where(x => !userIds.Contains(x.UserId));
score:4
That should certainly remove the elements, but I don't see how you would even notice whether it had or not?
First you create the list with ToList()
and then you call RemoveAll()
to remove some elements from that list, but since you haven't stored that list anywhere, you're just going to get the number of items removed back.
Source: stackoverflow.com
Related Articles
- Removing a single item from an enumerable source when the items are equal
- Using RemoveAll in a list of objects in C# not removing items
- How to Remove multiple items in List using RemoveAll on condition?
- removing items from a generic List<t>
- Removing sequential repeating items from List<T> using linq
- LINQ Source Code Available
- C# Code Contracts -- How to ensure that a collection of items contains items with unique properties?
- .NET 4 Code Contracts: "requires unproven: source != null"
- Enumerator stuck in endless loop when removing excess items from a List
- linq - how do you do a query for items in one query source that are not in another one?
- C# Linq query help removing foreach loops creating cleaner code
- List<T> RemoveAll() isn't removing items
- creating Linq to sqlite dbml from DbLinq source code
- Removing items in List using LINQ
- Removing items from a list where the Id is in another list
- Removing items from an IEnumerable which match items in a List using LINQ
- LINQ - Removing items in a List<T> that contain one item of an Array in any order or position
- removing items from list that exist in another list based on an ID member
- Code First EF: While searching database table is it possible to retrieve list of items it has in DataModel?
- source code for LINQ 101 samples
- Removing items from list in an async function
- Code Optimization to check some items of checkbox list
- removing items from a list based on two properties
- Removing Items from a list of objects when an object property is duplicated
- Difficulty in Removing Items From List?
- Linq RemoveAll removes all items
- Predicate condition to RemoveAll items off a list based on a condition not working
- Removing items in a list of list
- List or Array of String Contain specific word in Html Source Code
- Removing items in Jagged array using index
- Resharper conver to LINQ bug? Or is my code wrong
- randomize Select do not work
- Sitecore Content Search, converting to List<item> and editing
- OrderBy in List on the basis of Dictionary Key-Value
- asp.net mvc many-to-many relationships in linq to sql
- How to combine result of multiple Linq Expressions into one Expression?
- Automatically add some Where clauses to a Linq Expression Tree
- Nested foreach to LINQ in multi-level dictionary
- need to convert sql join query with count into linq and pass it to view
- How to break or Exit from Linq query
- How can I manage multiple OrderByDescending criteria?
- LINQ GroupBy x where there is more then one x and y is unique
- Reports VS 2010, drill down ICollection
- How to convert a list<string> or list<object> to a ListView.ListViewItemCollection in one line
- Is is possible to add .Where() on a child collection property using nhibernate linq?
- C# LINQ Z-Score query output to a Dictionary<string, SortedList<DateTime, double>>
- c# Linq group files by interval
- Understanding the => in LINQ
- Linq : Load parent and child entities
- Cannot perform LINQ complex object search in Entity Framework