2013-10-31 133 views
0

我將用示例解釋我的問題。如何更新linq查詢結果

可以說我有linq查詢結果。

var result1=from c in client 
    select new my_type 
    { 
     ... 
      stockDesctiption=?? 

    }; 

我們會說Client已提出所謂stockId。這與dammadgeStockHistory表中的stockId相同。這些表沒有連接任何外鍵約束。

如果我需要得到dammadgeStockHistory.stockDescrption爲每個客戶端如何做到這一點。

回答

1

加入表按這個領域(不需要外鍵約束):

var result1 = from c in client 
       join dsh in dammadgeStockHistory 
        on c.stockId equal dsh.stockId 
       select new my_type 
       { 
       stockId = c.stockId, 
       // ...     
       stockDesctiption = dsh.stockDescrption 
       }; 

更新,如果你想要做的 '左連接':

var result1 = from c in client 
       join dsh in dammadgeStockHistory 
        on c.stockId equal dsh.stockId into g 
       from cdsh in g.DefaultIfEmpty() 
       select new my_type 
       { 
       stockId = c.stockId, 
       // ...     
       stockDesctiption = cdsh == null ? null : dsh.stockDescrption 
       }; 
+1

dammageStockHistory,但+1很好的解決方案:) – gleng

+1

@gleng謝謝,固定錯字 –

+0

加入將只顯示誰擁有dammadgeStockHistory的客戶端。但我需要得到所有的客戶一起stockDescrption –