我正在尝试在 linq to entities (EF4) 中执行此查询
select Header.Id,
(select count(*)
from Detail
where Header.Id = Detail.headerId) detailcount
from Header
这行不通,因为它在 EF 中是不允许的:
(Header 和 Detail 是 EntityObjects)
from h in context.Header
select new Header
{
Id = h.Id,
DetailCount = (from d in context.Detail
where d.headerId = p.Id select d).Count()
}
DetailCount是我在Detail Entity(分部类)上新增的属性
上面的 Linq 查询不起作用,因为我无法投影到映射的实体上:
The entity cannot be constructed in a LINQ to Entities query
还有其他方法吗?
最佳答案
下面将为您完成任务,因为两者都是相关实体
from h in context.Header
select new Header
{
Id = h.Id,
detailCount = h.Detail.Count()
}
关于c# - Linq to Entities 计数子查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7175081/