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
有一个名为Products的实体,我想从产品中获取所有元素,但那些存在于associatedProducts.Products中
How can i do that ?
我怎样才能做到这一点 ?
3 个解决方案
#1
14
The following query works if associatedProducts list is fetched using EF in a previos query.
如果在previos查询中使用EF获取associatedProducts列表,则以下查询有效。
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);
否则,如果associatedProducts是一个尚未使用EF获取的列表(假设Key是一个整数);
List<int> tempIdList = associatedProducts.Select(q => q.Key ).ToList();
var temp = db.Products.Where(q => !tempIdList.Contains(q.Key));
#2
0
var query = from p in db.Products
where !(from a in associatedProducts.Products
select a.Products)
.Contains(p.Key)
select p;
I didn't test the query, but it should look like this.
我没有测试查询,但它应该是这样的。
You should have a look at how you can use "not in" in linq:
How would you do a "not in" query with LINQ?
您应该看看如何在linq中使用“not in”:如何使用LINQ进行“不在”查询?
#3
-3
You can load the list of products that you want to exclude, and then .Exclude()
it from the list of all products.
您可以加载要排除的产品列表,然后从所有产品列表中排除()它。
#1
14
The following query works if associatedProducts list is fetched using EF in a previos query.
如果在previos查询中使用EF获取associatedProducts列表,则以下查询有效。
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);
否则,如果associatedProducts是一个尚未使用EF获取的列表(假设Key是一个整数);
List<int> tempIdList = associatedProducts.Select(q => q.Key ).ToList();
var temp = db.Products.Where(q => !tempIdList.Contains(q.Key));
#2
0
var query = from p in db.Products
where !(from a in associatedProducts.Products
select a.Products)
.Contains(p.Key)
select p;
I didn't test the query, but it should look like this.
我没有测试查询,但它应该是这样的。
You should have a look at how you can use "not in" in linq:
How would you do a "not in" query with LINQ?
您应该看看如何在linq中使用“not in”:如何使用LINQ进行“不在”查询?
#3
-3
You can load the list of products that you want to exclude, and then .Exclude()
it from the list of all products.
您可以加载要排除的产品列表,然后从所有产品列表中排除()它。