gpt4 book ai didi

c# - 在 Entity Framework 中选择类别树

转载 作者:行者123 更新时间:2023-11-30 18:06:37 25 4
gpt4 key购买 nike

我有一个具有树结构的类别表 (Id,MasterId)我想选择属于一个类别和所有子类别的所有产品。

今天我使用这个有效的 SQL 查询,但我想添加分页,使用纯 LINQ 查询会更容易。我使用 Entity Framework 4。

@Count int = 100,
@CategoryId int

with mq as
(
select c.Id as parent, c.Id as child
from dbo.Categories c
where c.Id = @CategoryId
union all
select q.child, c.Id
from mq q
inner join dbo.Categories c on q.child = c.MasterId
)

select top (@Count) P.* from Products P
inner join ProductToCategory PC ON(PC.ProductId = P.Id)
where PC.CategoryId in (
select child from mq
)
and P.PublishStatus = 1
order by P.PublishedDate DESC;

关于如何通过分页(当前页面、每页产品数、产品总数)获得良好的 LINQ 查询有什么想法吗?

最佳答案

这是带有表表达式的递归/分层查询。 EF 不支持此类查询。如果您想通过到数据库的单次往返接收数据,您必须将其包装在存储过程中并将该过程导入您的 Entity Framework 模型。

使用 table expressions and ROW_NUMBER(). 时也可以在 SQL 中进行分页

关于c# - 在 Entity Framework 中选择类别树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4679582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com