我正在从具有多个连接和别名的SQL查询创建C#LINQ语句。我在构建LINQ时遇到了一些麻烦。
SQL:
SELECT
store.Name as 'Store',
store.CreatedOn as 'StoreCreated',
supplier.Name as 'Supplier',
supplier.CreatedOn as 'SupplierCreated',
farm.Name as 'Farm',
farm.CreatedOn as 'FarmCreated',
FROM Users store
INNER JOIN UserRelationship toSupplier on store.ID = toSupplier.YId
INNER JOIN Users supplier ON supplier.ID = toSupplier.XId
INNER JOIN UserRelationship toFarm ON store.ID = toFarm.XId
INNER JOIN Users farm ON farm.ID = toFarm.YId
WHERE
store.Active= '1'
AND
supplier.Active = '1'
AND
farm.Active = '1'
这将返回显示三方关系和日期之间关系的行。
所以,到目前为止,我有以下LINQ:
var newUserList = from store in Users
join toSupplier in UserRelationship on store.ID = toSupplier.YId
join supplier in Users on supplier.ID = toSupplier.XId
join toFarm in UserRelationship on store.ID = toFarm.XId
join farm in Users on farm.ID = toFarm.YId
我是否在正确的轨道上?任何帮助将不胜感激。
答案 0 :(得分:1)
LINQ join
语法在加入条件中使用equals
关键字而不是=
:
var newUserList = from store in Users
join toSupplier in UserRelationship on store.ID equals toSupplier.YId
join supplier in Users on supplier.ID equals toSupplier.XId
join toFarm in UserRelationship on store.ID equals toFarm.XId
join farm in Users on farm.ID equals toFarm.YId
select ...