连接两个表和多列的C#Linq语句
我有两张桌子;EndToEnd和PartPort。我想从EndToEnd中的同一行获取PartPortA和PartportB数据,并用它们查询Partport,然后从Partport中获取它们对应的PartGid,Partport可以位于Partport表中的任何一行上。到目前为止,我能够做到这一点,但我必须做两个不同的LINQ调用,但我想把它减少到一个。这是我的密码:连接两个表和多列的C#Linq语句,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我有两张桌子;EndToEnd和PartPort。我想从EndToEnd中的同一行获取PartPortA和PartportB数据,并用它们查询Partport,然后从Partport中获取它们对应的PartGid,Partport可以位于Partport表中的任何一行上。到目前为止,我能够做到这一点,但我必须做两个不同的LINQ调用,但我想把它减少到一个。这是我的密码: // this demonstrates how to join two tables, however only w
// this demonstrates how to join two tables, however only works for one AssetportGid at a time
var part_portGid_a_results = (from icp in entities.EndToEnd
where icp.IntertPortGidA != null &&
icp.IntertPortGidB != null
join ica in entities.PartPort
on icp.PartPortA equals ica.PortGid
select new { icp.PartPortA, ica.PartGid, }).ToList();
var part_portGid_b_results = (from icp in entities.EndToEnd
where icp.IntertPortGidA != null &&
icp.IntertPortGidB != null
join ica in entities.PartPort
on icp.PartPortB equals ica.PortGid
select new { icp.PartPortA, ica.PartGid, }).ToList();
return Json(part_portGid_a_results, JsonRequestBehavior.AllowGet);
我想做的是,我已经尝试过了,但得到的错误是:
var part_portGid_a_results = (from icp in entities.EndToEnd
where icp.IntertPortGidA != null &&
icp.IntertPortGidB != null
join ica in entities.PartPort
on icp.PartPortA && icp.PartPortB equals ica.PortGid
select new { icp.PartPortA, ica.PartGid, }).ToList();
我得到的错误是:
Guid? EndToEnd.PartPortB
Error:
Operator '&&' cannot be applied to operands of type 'System.Guid' and 'System.Guid?'
你不必使用join。如果你想加入一个“复杂”的比较,只需做一个笛卡尔乘积(from…from
),并通过where
子句链接行
var part_portGid_results = (from icp in entities.EndToEnd
where icp.IntertPortGidA != null &&
icp.IntertPortGidB != null
from ica in entities.PartPort
where icp.PartPortA == ica.PortGid
|| icp.PartPortB == ica.PortGid
select new { icp.PartPortA, ica.PartGid, }).ToList();
你不必使用join。如果你想加入一个“复杂”的比较,只需做一个笛卡尔乘积(from…from
),并通过where
子句链接行
var part_portGid_results = (from icp in entities.EndToEnd
where icp.IntertPortGidA != null &&
icp.IntertPortGidB != null
from ica in entities.PartPort
where icp.PartPortA == ica.PortGid
|| icp.PartPortB == ica.PortGid
select new { icp.PartPortA, ica.PartGid, }).ToList();
“on icp.PartPortA&&icp.PartPortB equals ica.PortGid”不正确,它不是有效的布尔表达式,因为icp.PartPortA是Guid,而不是布尔值。你想在两列上都加入吗?然后试试这个:“在icp.PartPortA等于ica.PortGid&&icp.PartPortB等于ica.PortGid”Intellisense似乎不喜欢这样,可能是因为一个是可空guid,另一个不是。。。(使用.Value nullable guid)“on icp.PartPortA&&icp.PartPortB equals ica.PortGid”不正确,它不是有效的布尔表达式,因为icp.PartPortA是guid,而不是布尔表达式。你想在两列上都加入吗?然后试试这个:“在icp.PartPortA等于ica.PortGid&&icp.PartPortB等于ica.PortGid”Intellisense似乎不喜欢这样,可能是因为一个是可空guid,另一个不是。。。(使用.Value可为空的guid)