Wes Doyle
Wes Doyle

Reputation: 2287

LINQ syntax for SQL query with multiple inner joins and aliases

I am working on creating a C# LINQ statement from a SQL query with multiple joins and aliases. I am having some trouble constructing the LINQ.

The 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'

This returns rows showing the relationships between the three parties and the dates.

So, far, I've got the following 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

Am I on the right track? Any help would be appreciated.

Upvotes: 1

Views: 3648

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125630

LINQ join syntax uses equals keyword instead of = in join condition:

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 ...

Upvotes: 3

Related Questions