我有一个使用点符号的Linq查询,它必须与几个不同的枚举进行连接。我发现进行多重连接的方法是一个接一个地执行:而不是相当于以下SQL语句的东西:
SELECT
blah blah blah
FROM
tableOne as One, tableTwo as Two, tableThree as Three
WHERE
One.fieldOne == Two.fieldOne AND
One.fieldTwo == Three.fieldTwo AND
One.fieldThree == something
它最终会像:
SELECT
blah blah blah
FROM
(
SELECT
blah blah blah
FROM
tableOne as One, tableTwo as Two
WHERE
One.fieldOne == Two.fieldOne
One.fieldThree == something
) as firstJoin,
tableThree as Three
WHERE
firstJoin.fieldTwo == Three.fieldTwo
Linq中的.Join()看起来像:
localTableOne.Join(localTableTwo,
One => One.fieldOne,
Two => Two.fieldOne,
(One, Two) => new { One, Two })
我遇到的问题是,一旦我进入第二个JOIN,前一个JOIN结果的结果的匿名变量没有属性“fieldOne”或“fieldTwo”或本地的任何字段桌子加入。它有一个属性“One”,它具有第一个表中的属性,以及属性“Two”,具有表2中的属性。因此,如果我想将这些结果与另一个表连接,则内部的lambda函数表格看起来像
firstJoinResult => firstJoinResult.One.fieldOne
如果我只加入三张桌子,这不会有问题。我目前正在加入7桌。 Linq查询以.Select()函数结束,因此lambda函数的字段最终看起来像:
fieldOne = seventhJoin.sixthJoin.fifthJoin.fourthJoin.thirdJoin.secondJoin.firstJoinResult.One.fieldOne
有什么方法可以让这个电话更小?在某种程度上访问我正在寻找的领域?另一种进行多重连接的方法是什么?
由于
UPD
我忘了提到我正在寻找使用点符号Linq的解决方案。
答案 0 :(得分:5)
使用查询语法,它看起来像这样
var query = from One in tableOne
from Two in tableTwo
from Three in tableThree
where One.fieldOne == Two.fieldOne
where One.fieldTwo == Three.fieldTwo
where One.fieldThree == something
select new {
};