Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove instances from a list by using LINQ or Lambda?

Tags:

c#

lambda

linq

Now I come a stage to get all my data as a list in cache(objects) and my next thing I have to do is to remove some instances from the list.

Normally, I would do removing like this:

List<T> list; List<T2> toBeRemovedItems; // populate two lists foreach(T2 item in toBeRemovedItems) {     list.Remove(delegate(T one) {          // build a condition based on item         // return true or false     }); } 

To be more specific, I actually build or populate toBeRemvoedItems list of a dynamic class (not a formal defined class). For example, the T class is something like MyClass and codes for removing are:

class MyClass<C> {     public string Value1 { get; set; }     public int Value2 { get; set; }     public C ObjectC { get; set; } } .... List<MyClass<C>> list; // populate list // populate toBeRemovedItems. Here is an example of hard-coded codes: var toBeRemovedLItems = new[] {     new { Value1="a", Value2 = 1},     new { Value2="x", Value2 = 10},     ... }; // toBeRemovedItems may be the result of Select from a collection foreach(var item in toBeRemovedLItems) {     list.Remove(delegate(MyClass one) {         return one.Value1 = item.Value1 && one.Value2 < item.Value2;     }); } 

I tried to search for Remove() method in IEnumerable interface from MSDN, but I cannot find the method of Remove() there (it makes sense that IEnumerable is used just for enumeration). In List class, there are several overloaded Remove(...) methods. I am not sure if there any alternative ways to remove items from a list by using LINQ or Lambda expressions?

By the way, I thought about a way to do a query against a list to get a subset or a new IEnumerable list with Where conditions, similar as moving items from a list. However, I prefer to remove items from my cached list, and there some cases I just cannot reset list property in a class to a new list (private set for example).

like image 275
David.Chu.ca Avatar asked Jul 26 '09 19:07

David.Chu.ca


People also ask

Is LINQ faster than lambda?

There is no performance difference between LINQ queries and Lambda expressions.

What does => mean in LINQ?

The => operator can be used in two ways in C#: As the lambda operator in a lambda expression, it separates the input variables from the lambda body. In an expression body definition, it separates a member name from the member implementation.

How do I delete a record in LINQ?

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. LINQ to SQL does not support or recognize cascade-delete operations.


2 Answers

You could use the method RemoveAll:

MyClass one; //initialize MyClass list.RemoveAll(item => one.Value1 == item.Value1 && one.Value2 < item.Value2); 
like image 82
Francis B. Avatar answered Sep 30 '22 13:09

Francis B.


You can use LINQ's Where method to filter out values that should not be a part of the list. The result is an IEnumerable<T> with the elements removed.

var res = list.Where(item => !(one.Value1 == item.Value1 && one.Value2 < item.Value2)); 

This will not updated the original List<T> instance but instead will create a new IEnumerable<T> with the values removed.

like image 35
JaredPar Avatar answered Sep 30 '22 13:09

JaredPar