Get everything except the SQL database using Entity Framework

I have a list of products like this

var r = db.Products.Where(x => x.Sites .Where(z => z.Key == associatedProducts.Key) .Any() ).ToList() 

There is an object called "Products", I want to get all the elements from the products, except those that exist in related products. Products

How can i do this?

+7
source share
3 answers

The following query works if a list of related products is retrieved using EF in the previos query.

 var temp = db.Products.ToList().Except(associatedProducts).ToList(); 

otherwise, if associatedProducts is a list that was not selected using EF (it is assumed that Key is an integer);

 List<int> tempIdList = associatedProducts.Select(q => q.Key ).ToList(); var temp = db.Products.Where(q => !tempIdList.Contains(q.Key)); 
+14
source
  var query = from p in db.Products where !(from a in associatedProducts.Products select a.Products) .Contains(p.Key) select p; 

I have not tested the request, but it should look like this.

You should see how you can use "not in" in linq:
How would you make a "not in" query with LINQ?

0
source

You can download the list of products that you want to exclude, and then .Exclude() from the list of all products.

-3
source

All Articles