I see a collection has a .Remove() and a .RemoveAt() method. I would like to do something like this:
myObject.ChildList.RemoveWhere(r=>r.Name == "Joe");
What is the best way to achieve this besides doing a seperate where and then a loop through each item and them calling .Remove()
You can delete rows in a database by removing the corresponding LINQ to SQL objects from their table-related collection. LINQ to SQL translates your changes to the appropriate SQL DELETE commands.
The Any operator is used to check whether any element in the sequence or collection satisfy the given condition. If one or more element satisfies the given condition, then it will return true. If any element does not satisfy the given condition, then it will return false.
LINQ Select operator is used to return an IEnumerable collection of items, including the data performed on the transformation of the method. By using Select Operator, we can shape the data as per our needs. In it, we can use two syntax types; let's see each method working flow.
List<T>
has a RemoveAll
method that accepts a predicate.
myObject.ChildList.RemoveAll(r => r.Name == "Joe");
You could write RemoveWhere()
as an extension method. But changing collections is tricky and sometimes very inefficient.
Why not create a new collection?
myObject.ChildList = myObject.ChildList.Except(
myObject.ChildList.Where(r=>r.Name == "Joe"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With