I need the equivalent of this SQL statement in Linq, using method/fluent syntax.
SELECT u.[UserId], s.[UserId], d.[UserId]
FROM dbo.[Attachment] z
INNER JOIN dbo.[Activity] a ON z.[ActivityId] = a.[ActivityId]
INNER JOIN dbo.[Case] c ON a.[CaseId] = c.[CaseId]
INNER JOIN dbo.[CaseUser] x ON c.[CaseId] = x.[CaseId]
INNER JOIN dbo.[User] u ON x.[UserId] = u.[UserId]
LEFT OUTER JOIN dbo.[User] s ON u.[SupervisorId] = s.[UserId]
LEFT OUTER JOIN dbo.[User] d ON s.[SupervisorId] = d.[UserId]
WHERE u.[UserId] = @x OR s.[UserId] = @x OR d.[UserId] = @x
Also, I use it in a context where I must return a System.Linq.Expressions.Expression object. For example, an example of existing, simpler code would be:
public override Expression<Func<Attachment, bool>> MatchingCriteria
{
get { return a => a.Activity.Case.CaseUsers.Any(x => (x.User.Id == this.id)); }
}
I am stumped by the left joins using method syntax that evaluates to a bool.
I think the following should work… You check to make sure the related entity in the model is not null and then do a comparison if it is not: