I have the following query that I'm trying to translate into a linq statement:
SELECT DISTINCT
rc.RoleCatID, rc.RoleCategoryDescription
FROM
dbo.[Role] r
INNER JOIN
dbo.Employee e ON r.RoleID = e.RoleID
INNER JOIN
dbo.RoleCategory rc ON r.RoleCatID = rc.RoleCatID
INNER JOIN
dbo.Product p ON rc.ProductID = p.ProductID
WHERE
p.ChannelID = '123456'
AND (p.[ProductID] = 'abc' OR p.[ProductID] = 'def' OR p.[ProductID] = 'ghi')
I have the below so far, but I can't reference anything beyond the original table's fields in the where
clause. Intermediate selects
haven't helped either.
db.Roles.Join(db.Employees, r => r.RoleID, e => e.RoleID, (r, e) => r)
.Join(db.RoleCategories, r => r.RoleCatID, rc => rc.RoleCatID, (r, rc) => r)
.Join(db.Products, rc => rc.ProductID, p => p.ProductID, (rc, p) => rc)
.Where(p => p.ChannelID == "123456" && (p.ProductID == "abc" || p.ProductID == "def" || p.ProductID == "ghi")); // <-- this line fails
It can't see any of the Product
table's fields and treats p
as Role
object from the Role
table. The last join is also compromised as it should be:
.Join(db.Products, rc => rc.ProductID, p => p.ProductID, (rc, p) => rc)
However rc
is treated as a Role
object instead of a RoleCategory
object.