如何过滤 EF Core 中的多对多联接
Posted
技术标签:
【中文标题】如何过滤 EF Core 中的多对多联接【英文标题】:How to filter many to many joins in EF Core 【发布时间】:2021-11-16 09:49:08 【问题描述】:我的结构如下:
产品类别 产品(具有一个产品类别) ProductComplementCategory(产品有很多 ProductComplementCategory) ComplementCategory(ProductComplementCategory 有一个 ComplementCategory) ComplementCategoryComplements(ComplementCategory 有很多 ComplementCategoryComplements) Complement(ComplementCategoryComplements 有一个 Complement)所有表都具有“活动”属性,我需要在产品类别、产品、补充类别和补充表中选择所有具有连接但过滤活动 = 1 的表
SQL 查询:
SELECT
pc.*,
p.*,
cc.*,
c.*
FROM
ProductCategory pc
JOIN Product p ON pc.[ Uid ] = p.ProductCategoryId
JOIN ProductComplementCategory pcc ON p.[ Uid ] = pcc.ProductID
JOIN ComplementCategory cc ON pcc.ComplementCategoryID = cc.[ Uid ]
JOIN ComplementCategoryComplements ccc ON cc.[ Uid ] = ccc.ComplementCategoryID
JOIN Complement c ON ccc.ComplementID = c.[ Uid ]
WHERE
pc.Active = 1
AND p.Active = 1
AND cc.Active = 1
AND c.Active = 1
我需要在 EF Core 中执行此查询!
【问题讨论】:
docs.microsoft.com/en-us/ef/core/modeling/… & docs.microsoft.com/en-us/ef/core/querying/filters 【参考方案1】:希望对你有帮助
var temp = (
from pc in ProductCategory
join p in Product
on pc.Uid equals p.ProductCategoryId
join pcc in ProductComplementCategory
on pcc on p.Uid equals pcc.ProductID
join permission in ComplementCategory
on pcc.ComplementCategoryID equals cc.Uid
join cc in ComplementCategoryComplements
on cc.Uid equals ccc.ComplementCategoryID
join c in Complement
on ccc.ComplementID equals c.Uid
where pc.Active = 1 && p.Active = 1 && cc.Active = 1 && c.Active = 1
select new pc,p,pcc,c,cc
).ToList
【讨论】:
以上是关于如何过滤 EF Core 中的多对多联接的主要内容,如果未能解决你的问题,请参考以下文章