LINQ Join里面Join,如何?
我是 LINQ 新手。我正在使用 LINQ to Objects(我认为),并且数据的设置方式我无法直接获取我需要的数据。 这是我需要做的事情的一般结构:
FROM Project
LEFT OUTER JOIN TechnologySectors
LEFT OUTER JOIN SelectedAgencies
LEFT OUTER JOIN ProjectStatus
JOIN Process
我需要来自 Process 的单个数据。
到目前为止,我已经弄清楚如何使用 DefaultIfEmpty()
与 LINQ 进行 LEFT OUTER JOIN,但我无法弄清楚如何使用 ProjectStatus 获取 Process to JOIN。
到目前为止,我有这个(ps 是 ProjectStatus):
join ec in this._Process.GetProcessList() on ps.ProcessID equals ec.ProcessID into psec
但这给了我一个关于“ps 不在 equals 左侧范围内”的错误。
编辑
出于参考目的,我包含的“连接”并不是完整的语句。 “ProjectStatus”(ps) 已加入“Project”(pr),我还需要加入“Process”(ec)。
ec 与 pr 没有任何直接关系,因此必须通过 ps 连接。 翻转“on”语句并不能解决问题。
编辑2
完整的 LINQ 查询:
from pr in this._projectRepo.GetAllProjects()
join tr in this._techRepo.GetTechnologySectors() on pr.TechnologySectorID equals tr.TechnologySectorID into prtr
join ev in this._ecEnvRepo.GetAllSelectedAgencies() on pr.ID equals ev.ID into prev
join ps in this._ecProjectStatRepo.GetAllECProjectStatus() on pr.ID equals ps.ID into prps
***THIS LINE***join ec in this._ecProcessRepo.GetProcessList() on ps.ProcessID equals ec.ProcessID into psec
from tr in prtr.DefaultIfEmpty()
from ev in prev.DefaultIfEmpty()
from ps in prps.DefaultIfEmpty()
from ec in psec.DefaultIfEmpty()
这不起作用。
我也尝试取出该行并仅使用此行:
from ec in this._ecProcessRepo.GetProcessList() where (ec.ProcessID == ps.ProcessID)
并且我尝试使用此行而不是 ps 和 ec 行:
from ps in this._ecProjectStatRepo.GetAllECProjectStatus() where (ps.ID == pr.ID)
join ec in this._ecProcessRepo.GetProcessList() on ps.ProcessID equals ec.ProcessID into psec
from ec in psec.DefaultIfEmpty()
I am new to LINQ. I am using LINQ to Objects (I think) and the way the data is set up I can't directly get to a piece of data I need.
This is the general structure of what I NEED to do:
FROM Project
LEFT OUTER JOIN TechnologySectors
LEFT OUTER JOIN SelectedAgencies
LEFT OUTER JOIN ProjectStatus
JOIN Process
I need a single piece of data from Process.
So far I have figured out how to do a LEFT OUTER JOIN with LINQ using DefaultIfEmpty()
but I cannot figure out out to get Process to JOIN with ProjectStatus.
So far I have this (ps is ProjectStatus):
join ec in this._Process.GetProcessList() on ps.ProcessID equals ec.ProcessID into psec
but that gives me an error about "ps not in scope on left side of equals".
EDIT
For reference sake, the "join" that I included is not the entire statement. "ProjectStatus" (ps) is joined to the "Project" (pr) and I need "Process" (ec) joined as well.
ec does not have any relationship directly to pr and so it must be joined through ps.
Flipping the "on" statements doesn't solve the problem.
EDIT 2
The full LINQ query:
from pr in this._projectRepo.GetAllProjects()
join tr in this._techRepo.GetTechnologySectors() on pr.TechnologySectorID equals tr.TechnologySectorID into prtr
join ev in this._ecEnvRepo.GetAllSelectedAgencies() on pr.ID equals ev.ID into prev
join ps in this._ecProjectStatRepo.GetAllECProjectStatus() on pr.ID equals ps.ID into prps
***THIS LINE***join ec in this._ecProcessRepo.GetProcessList() on ps.ProcessID equals ec.ProcessID into psec
from tr in prtr.DefaultIfEmpty()
from ev in prev.DefaultIfEmpty()
from ps in prps.DefaultIfEmpty()
from ec in psec.DefaultIfEmpty()
That does not work.
I have also tried taking out that line and just using this:
from ec in this._ecProcessRepo.GetProcessList() where (ec.ProcessID == ps.ProcessID)
And I have tried using this instead of the ps and ec lines:
from ps in this._ecProjectStatRepo.GetAllECProjectStatus() where (ps.ID == pr.ID)
join ec in this._ecProcessRepo.GetProcessList() on ps.ProcessID equals ec.ProcessID into psec
from ec in psec.DefaultIfEmpty()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需要将
on
语句join ec in this._Process.GetProcessList() on ec.ProcessID equals ps.ProcessID into psec
关于多个连接,您应该能够链接他们
You just need to flip the
on
statementjoin ec in this._Process.GetProcessList() on ec.ProcessID equals ps.ProcessID into psec
In regards to the multiple joins you should be able to chain them
您只需要交换
ec
和ps
要进行左连接,您需要执行以下操作
有多种方法可以命名对象,但我通常发现我写的是join 以进入带有 nullable 前缀的对象名称,然后使用下一行
from ec in nullablePsec.DefaultIfEmpty()
编辑:在看到完整的 linq 查询,您需要像这样加入
注意,这里的连接位于 prps 变量名称上,因为您在上面的行中选择到 prps 中
into prps
已更改了您将要使用的变量名称在连接查询中使用到进程列表。编辑2
如果你将语句写成如下,可能效果会更好一些
You just need to swap the
ec
andps
aroundTo do the left join you need to do the following
There are various ways to name the objects but I usually find I write the join to go into the object name with nullable prefixed and then give it the same name again by using the next line
from ec in nullablePsec.DefaultIfEmpty()
EDIT: After seeing the full linq query you need to be joining like this
Note here the join is on the prps variable name as you are selecting into to prps in the line above it
The
into prps
has changed the variable name that you will be working with in the join query onto the process list.EDIT 2
It might work a bit better if you write the statement as follows
当您进行组连接时,内部序列中的变量超出范围,您将无法再访问各个元素。如果您想要访问权限或不首先加入组,则需要将关联的
DefaultIfEmpty()
向上移动。When you do a group join, the variable from the inner sequence goes out of scope and you no longer have access to the individual elements. You need to move the associated
DefaultIfEmpty()
up if you want that access or not do the group join in the first place.