我想知道如何在通过多个字段连接两个表时在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