2016-08-23 77 views
0

我有兩個表; EndToEnd和PartPort。我想從EndToEnd中的同一行中獲取PartPortA和PartportB數據,並使用它們查詢Partport,並從Partport中獲取其可能位於Partport表中任何行的PartGid。到目前爲止,我可以做到這一點,但我必須做兩個不同的LINQ調用,但我想將其降低到一個。這裏是我的代碼:C#Linq語句加入兩個表和多列

// 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?' 
+0

鏈接行「上icp.PartPortA && icp.PartPortB等於ica.PortGid」不正確,它不是一個有效的布爾表達式,因爲icp.PartPortA是一個Guid,而不是布爾值。你是否想加入兩個欄目?然後試試這個,而不是:「on icp.PartPortA等於ica.PortGid && icp.PartPortB等於ica.PortGid」 – HaukurHaf

+0

智能感知似乎不喜歡那 – DeeTee

+0

可能是因爲一個是可以爲空的GUID,另一個不是...(使用.Value可空的guid) – HaukurHaf

回答

2

你不要使用加入。如果你想用「複雜」比較的加入,只是做一個笛卡爾乘積(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();