Get All Except from SQL database using Entity Framework

18,093

The following query works if associatedProducts list is fetched using EF in a previos query.

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

otherwise, if associatedProducts is a list which has not been fetched using EF (assuming Key is an integer);

List<int> tempIdList = associatedProducts.Select(q => q.Key ).ToList();
var temp = db.Products.Where(q => !tempIdList.Contains(q.Key));
Share:
18,093
Mohamed Naguib
Author by

Mohamed Naguib

I never fear death or dying ... I only fear never trying I am whatever I am ... only god can judge me now

Updated on June 05, 2022

Comments

  • Mohamed Naguib
    Mohamed Naguib almost 2 years

    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 entity called Products, I want to get all elements from products except those exist in associatedProducts.Products

    How can i do that ?