LINQ Join里面Join,如何?

发布于 2024-10-19 05:53:16 字数 1844 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

日暮斜阳 2024-10-26 05:53:16

您只需要将 on 语句

join ec in this._Process.GetProcessList() on ec.ProcessID equals ps.ProcessID into psec

关于多个连接,您应该能够链接他们

You just need to flip the on statement

join 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

花伊自在美 2024-10-26 05:53:16

您只需要交换 ecps

ec in this._Process.GetProcessList() on 
    ps.ProcessID equals ec.ProcessID into psec

要进行左连接,您需要执行以下操作

ec in this._Process.GetProcessList() on 
    ps.ProcessID equals ec.ProcessID into nullablePsec
from ec in nullablePsec.DefaultIfEmpty()

有多种方法可以命名对象,但我通常发现我写的是join 以进入带有 nullable 前缀的对象名称,然后使用下一行 from ec in nullablePsec.DefaultIfEmpty()

编辑:在看到完整的 linq 查询,您需要像这样加入

ec in this._Process.GetProcessList() on 
    prps.ProcessID equals ec.ProcessID into psec

注意,这里的连接位于 prps 变量名称上,因为您在上面的行中选择到 prps 中

join ps in this._ecProjectStatRepo.GetAllECProjectStatus() on pr.ID equals ps.ID into prps

into prps 已更改了您将要使用的变量名称在连接查询中使用到进程列表。

编辑2
如果你将语句写成如下,可能效果会更好一些

from pr in this._projectRepo.GetAllProjects()
join tr in this._techRepo.GetTechnologySectors() on pr.TechnologySectorID equals tr.TechnologySectorID into prtr
from tr in prtr.DefaultIfEmpty()
join ev in this._ecEnvRepo.GetAllSelectedAgencies() on pr.ID equals ev.ID into prev
from ev in prev.DefaultIfEmpty()
join ps in this._ecProjectStatRepo.GetAllECProjectStatus() on pr.ID equals ps.ID into prps
from ps in prps.DefaultIfEmpty()
join ec in this._ecProcessRepo.GetProcessList() on ps.ProcessID equals ec.ProcessID into psec
from ec in psec.DefaultIfEmpty()

You just need to swap the ec and ps around

ec in this._Process.GetProcessList() on 
    ps.ProcessID equals ec.ProcessID into psec

To do the left join you need to do the following

ec in this._Process.GetProcessList() on 
    ps.ProcessID equals ec.ProcessID into nullablePsec
from ec in nullablePsec.DefaultIfEmpty()

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

ec in this._Process.GetProcessList() on 
    prps.ProcessID equals ec.ProcessID into psec

Note here the join is on the prps variable name as you are selecting into to prps in the line above it

join ps in this._ecProjectStatRepo.GetAllECProjectStatus() on pr.ID equals ps.ID into prps

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

from pr in this._projectRepo.GetAllProjects()
join tr in this._techRepo.GetTechnologySectors() on pr.TechnologySectorID equals tr.TechnologySectorID into prtr
from tr in prtr.DefaultIfEmpty()
join ev in this._ecEnvRepo.GetAllSelectedAgencies() on pr.ID equals ev.ID into prev
from ev in prev.DefaultIfEmpty()
join ps in this._ecProjectStatRepo.GetAllECProjectStatus() on pr.ID equals ps.ID into prps
from ps in prps.DefaultIfEmpty()
join ec in this._ecProcessRepo.GetProcessList() on ps.ProcessID equals ec.ProcessID into psec
from ec in psec.DefaultIfEmpty()
樱&纷飞 2024-10-26 05:53:16

当您进行组连接时,内部序列中的变量超出范围,您将无法再访问各个元素。如果您想要访问权限或不首先加入组,则需要将关联的 DefaultIfEmpty() 向上移动。

var 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
            from ps in prps.DefaultIfEmpty()
            // you need to resolve `ps == null` issues here
            let key = ps == null ? -1 : ps.ProcessID
            join ec in this._ecProcessRepo.GetProcessList()
                on key equals ec.ProcessID
                into psec
            from tr in prtr.DefaultIfEmpty()
            from ev in prev.DefaultIfEmpty()
            from ec in psec.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.

var 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
            from ps in prps.DefaultIfEmpty()
            // you need to resolve `ps == null` issues here
            let key = ps == null ? -1 : ps.ProcessID
            join ec in this._ecProcessRepo.GetProcessList()
                on key equals ec.ProcessID
                into psec
            from tr in prtr.DefaultIfEmpty()
            from ev in prev.DefaultIfEmpty()
            from ec in psec.DefaultIfEmpty()
            // ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文