获取所有除使用Entity Framework的SQL数据库
问题描述:
我有这样的产品列表
var r = db.Products.Where(x => x.Sites
.Where(z => z.Key == associatedProducts.Key)
.Any()
).ToList()
有一个实体,称为产品,我想从除产品的所有元素associatedProducts.Products存在
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 ?
答
下面的查询工作如果associatedProducts名单是牵强的previos查询中使用EF
The following query works if associatedProducts list is fetched using EF in a previos query.
var temp = db.Products.ToList().Except(associatedProducts).ToList();
否则,如果 associatedProducts
是一个列表,其中一直没有取出使用EF(假设键
是一个整数);
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));