Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ: How to remove element from IQueryable<T>

How do you loop through IQueryable and remove some elements I don't need.

I am looking for something like this

var items = MyDataContext.Items.Where(x => x.Container.ID == myContainerId);
foreach(Item item in items)
{
  if(IsNotWhatINeed(item))
    items.Remove(item); 
}

Is it possible? Thanks in advance

like image 478
Aximili Avatar asked May 30 '10 04:05

Aximili


People also ask

What is the difference between IEnumerable T and IQueryable T?

The major difference between IQueryable and IEnumerable is that IQueryable executes query with filters whereas IEnumerable executes the query first and then it filters the data based on conditions.

What inherits from IQueryable?

The IQueryable interface inherits the IEnumerable interface so that if it represents a query, the results of that query can be enumerated. Enumeration causes the expression tree associated with an IQueryable object to be executed.

What is difference between IEnumerable and IQueryable in LINQ?

In LINQ to query data from database and collections, we use IEnumerable and IQueryable for data manipulation. IEnumerable is inherited by IQueryable, Hence IQueryable has all the features of IEnumerable and except this, it has its own features. Both have its own importance to query data and data manipulation.

What is IQueryable interface?

The Iterable interface was introduced in JDK 1.5. It belongs to java. lang package. In general, an object Implementing Iterable allows it to be iterated. An iterable interface allows an object to be the target of enhanced for loop(for-each loop).


2 Answers

You should be able to query that further as in this

var filtered = items.Where(itm => IsWhatINeed(itm));

Also notice the subtle change in the boolean function to an affirmative rather than a negative. That (the negative) is what the not operator is for.

like image 68
Anthony Pegram Avatar answered Sep 19 '22 11:09

Anthony Pegram


items = items.Where( x => !IsNotWhatINeed(x) );
like image 43
Fyodor Soikin Avatar answered Sep 17 '22 11:09

Fyodor Soikin