2011-03-23 24 views
0

HI,有加入多個表,得到的DataRow輸出在LINQ

我遇到的情況,我有加入多個表,得到的DataRow輸出(所有行由查詢返回)。

SQL查詢:

SELECT Fr.InterCodeId 
     FROM  
     CodeShareInterline Fr,  
     Airline A,Zone Z # 
     WHERE  
     A.AirlineId = Fr.AirlineId 
     And Fr.ContractId=Z.ContractId 

我知道如何執行加入LINQ,但我如何能在LINQ的select語句選擇所有的列(行)。

回答

0
var result = from fr in dataContext.CodeShareInterline 
      from a in dataContext.AirLine 
      from z in dataContext.Zone 
      where a.AirlineId == fr.AirlineId && fr.ContractId == z.ContractId 
      select new 
       { 
        Interline = fr, 
        AirLine = a, 
        Zone = z 
       }; 

匿名類型包含要的所有數據,你可以很容易地訪問一個列:

result.FirstOrDefault().Zone.SomeField 
+1

我認爲在這兩個您的解決方案你缺少了他的要求,投影到的DataRow。 – mmix 2011-03-23 11:18:02

0

這是未經測試,但接近這應該工作。假設你的數據上下文是調用Context。這是你上面的翻譯。

var o = from fr in Context.CodeShareInterline 
      join a from Context.Airline on a.AirlineId == fr.AirlineId 
      join z from Context.Zone on fr.ContactId == z.ContactId 
      select fr.InterCodeId; 

如果你想選擇所有的數據,那麼你需要做這樣的事情。

var o = from fr in Context.CodeShareInterline 
      join a from Context.Airline on a.AirlineId == fr.AirlineId 
      join z from Context.Zone on fr.ContactId == z.ContactId 
      select new { 
         Interline = fr, 
         AirLine = a, 
         Zone = z 
         };