Linq查询以查找所有孤儿产品 [英] Linq query to find all orphan products
本文介绍了Linq查询以查找所有孤儿产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于产品和产品类别系统,我有三种模型:
I have three models for a product and product category system:
[Products]
Id
Title
Info
Price
[ProductCategories]
Id
ParentId
SortOrder
Title
[ProductsInCategories]
Id
ProductId
ProductCategoryId
SortOrder
现在,我要列出在ProductsInCategories表中没有条目的所有产品的列表.即所有孤儿产品.如何在linq中做到这一点?
Now, I want to make a list of all products which doesn't have an entry in the ProductsInCategories table. I.e., all orphan products. How can I do that in linq?
推荐答案
您可以使用!Any
来仅获取在 ProductsInCategories
中没有条目的那些人:
You can use !Any
to get only those without an entry in ProductsInCategories
:
List<Product> productList = db.Products
.Where(p => !db.ProductsInCategories.Any(pc => p.Id == pc.ProductId))
.ToList();
这篇关于Linq查询以查找所有孤儿产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文