我想知道如何在通過多個字段連接兩個表時在Linq中編寫內部連接。LINQ使用點符號和多個連接字段加入
例如,說這是SQL相當於:
SELECT tableOne.fieldThree
FROM table_One AS tableOne,
table_Two AS tableTwo,
WHERE
tableOne.fieldOne == tableTwo.fieldOne AND
tableOne.fieldTwo == tableTwo.fieldTwo;
我嘗試這樣做:
tableTwo.Join(tableOne,
two => new { two.fieldOne, two.fieldTwo },
one => new { one.fieldOne, one.fieldTwo },
(two, one) => one.fieldThree)
.ToList();
但是,編譯器顯示,說的方法不能從使用推斷錯誤。
謝謝。
您的示例代碼中有一個流氓關閉大括號,一件事。請向我們展示您嘗試的*確切*代碼,以及它帶來的問題。哦,我懷疑在最後一個參數中,你的意思是'one.fieldThree'而不是'tableOne.fieldThree'。一個簡短的*完整的*示例(使用數據模型)將使它更容易幫助你。 –
看看http://stackoverflow.com/questions/373541/how-to-do-joins-in-linq-on-multiple-fields-in-single-join –
@JonSkeet:改正。 – Heathcliff