Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linq remove items from query where any list value is present

I am trying to retrieve this in linq but can't seem to figure it out. I want to filter a query based on if a value in the query exist in a list but remove those items from the query.

Let say I have a list of ids

List<int> UserIds = new List<int>(); //contains 1 2 3

var query = MyTable.Where(a=>a.Id.Notexist(UserIds))

basically I would want to remove all items from the UserId list from the query) so query should not return items with Id = 1,2, or 3

like image 899
Jake Avatar asked Jun 22 '13 21:06

Jake


People also ask

How to Remove particular item from List based on condition in c#?

C# | Remove all elements of a List that match the conditions defined by the predicate. List<T>. RemoveAll(Predicate<T>) Method is used to remove all the elements that match the conditions defined by the specified predicate.

How do I delete a record in LINQ query?

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.


1 Answers

Is this what you're after?

MyTable.Where(a => !UserIds.Contains(a.Id))

This will select everything from MyTable where the Id is not in UserIds.

like image 92
Ant P Avatar answered Oct 12 '22 23:10

Ant P