如何在域服务类中制定联接查询?
在域服务类中,我想在联接查询中使用两个实体:WorkerOnMachine {id(int), WorkerId(int), MachineId(int)} 和 Worker(WorkerId(int), Name(string).. 。)
我想在域服务类中创建一个方法,该方法按以下方式返回在选定机器上运行的所有工作人员(工作人员-机器关系存储在 WorkerOnMachineTable 中):
public IQueryable<Worker> GetWorkerByMachineId(int machineId)
{
var joinedTable = from wom in this.ObjectContext.WorkerOnMachine
join w in this.ObjectContext.Worker on wom.WorkerId equals w.Id
where wom.MachineId == machineId
select new { w };
return joinedTable as IQueryable<Worker>;
}
但该方法返回一个空列表。有谁知道在域服务类中编写上述方法的正确方法是什么?
谢谢你!
in the domain service class there are two entities which I would like to use in join query: WorkerOnMachine {id(int), WorkerId(int), MachineId(int)} and Worker(WorkerId(int), Name(string)...)
I wanted to create a method in domain service class which returned all workers that operate on selected machine (the worker-machine relationships are stored in WorkerOnMachineTable) the following way:
public IQueryable<Worker> GetWorkerByMachineId(int machineId)
{
var joinedTable = from wom in this.ObjectContext.WorkerOnMachine
join w in this.ObjectContext.Worker on wom.WorkerId equals w.Id
where wom.MachineId == machineId
select new { w };
return joinedTable as IQueryable<Worker>;
}
but the method returns an empty list. Does anyone know what is the right way to write the above method in domain service class?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用 var,它会让你感到困惑。
正如所写,joinedTables 是 IQueryable的实例。由于它不是
IQueryable
,as
的转换返回 null。相反,写:
然后修复:
最后:
(顺便说一句,那个连接看起来不错)。
Don't use var, it is confusing you.
As written, joinedTables is an instance of
IQueryable<AnonType>
. Since it is not anIQueryable<Worker>
the cast byas
returns null.Instead, write:
Then fix:
and finally:
(btw, that join looks good).