我正在使用 linq to sql - 我正在尝试获取以下 2 个道具的计数(打开和关闭任务)但是我收到一个异常,说我无法在查询中加载实体,我该如何实现?
我的代码:
var milestones = ReadOnlySession.All<Milestone>()
.Where(x => x.InstructionID == instructionid)
.OrderBy(x => x.Name)
.Select(x => new Milestone
{
Name = x.Name,
InstructionID = x.InstructionID,
Body = x.Body,
Deadline = x.Deadline,
MilestoneID = x.MilestoneID,
TotalClosedTasks =
ReadOnlySession.All<InstructionTask>()
.Count(c => c.Milestone == x.MilestoneID && !c.IsOpen),
TotalOpenTasks =
ReadOnlySession.All<InstructionTask>()
.Count(c => c.Milestone == x.MilestoneID && c.IsOpen)
})
.ToList();
错误:
消息:不允许在查询中显式构造实体类型“Surventrix.Domain.Model.Entities.Milestone”。
来源:System.Data.Linq
注意: ReadOnlySession - 对数据上下文的抽象,为我提供了 All、Single(基本上获取 T 的所有实体)