我正在尝试在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()
}
我在 Detail 实体(部分类)中添加了一个新属性 DetailCount。
上述 Linq 查询无法工作,因为我不能将其投影到映射实体上:
The entity cannot be constructed in a LINQ to Entities query
还有其他的方法吗?