I'm learning Entity Framework and attempting to convert an existing SQL query to a LINQ query, but struggling to convert it.
SELECT taskItems.Description,taskItemResponses.IsCompleted,TaskItemResponses.userId,TaskItemResponses.Notes
FROM TaskLists
LEFT JOIN TaskItems ON TaskLists.TaskListId = TaskItems.TaskListId
LEFT JOIN TaskItemResponses ON TaskItemResponses.TaskItemId = TaskItems.TaskItemId
AND TaskItemResponses.UserId = '1'
This works fine for me, it brings back the following data, always showing the list of Tasks, and if a user has responded to any of them, if they've completed it and what notes they've added.
Description IsCompleted userId Notes
Task A NULL NULL NULL
Task B NULL NULL NULL
Task C NULL NULL NULL
Task D 1 1 I've done this now.
Task E NULL NULL NULL
But when I'm trying to convert this to a LINQ query within C# I can't figure out the syntax, so far i've got
var query = from t in DbContext.TaskList
join ti in DbContext.TaskItem on t.TaskListId equals ti.TaskListId
join tr in DbContext.TaskItemResponse on ti.TaskItemId equals tr.TaskItemId into trj
from x in trj.DefaultIfEmpty()
where x.UserId == userId
select t;
But this isn't filtering for a particular UserId, and instead returns
Description IsCompleted userId Notes
Task A 0 2 Great
Task B 1 2 Okay
Task C 1 3 Nope
Task D 1 1 I've done this now.
Task E 0 5 Ok.