var TblPolls = new[]
{
new { Guid = 1, Question = "question 1", UserId = 1 },
new { Guid = 2, Question = "question 2", UserId = 2 },
new { Guid = 3, Question = "question 3", UserId = 1 },
};
var TblVotes = new[]
{
new { VoteId = 1, UserId = 1, PollGuid = 1, Value = "No" },
new { VoteId = 2, UserId = 2, PollGuid = 1, Value = "Yes" },
new { VoteId = 3, UserId = 3, PollGuid = 1, Value = "Yes" },
new { VoteId = 4, UserId = 1, PollGuid = 2, Value = "No" },
};
I have these tables above, but as you can see that question 3 has not been voted. So how do i still pull out the data for TblPolls row for Question 3, taking into account that there are no votes for this Poll, so the result should be that the row question 3 is returned but the values in votes are null. Im guessing this is kind of a left join in linq. I have tried this but im not getting back the results i want:
poll = (
from polls in ctx.TblPolls
join votes in ctx.TblVotes on polls.Guid equals votes.PollGuid into gg
where polls.Guid == guid
from ff in gg.DefaultIfEmpty()
where (ff.UserId == userId || ff.UserId == null) && ff.PollGuid == guid
select new
{
polls.UserId,
polls.Id,
polls.Guid,
polls.Question,
polls.Description,
polls.Date,
polls.VideoId,
value = gg.Where(x => x.UserId == userId && x.PollGuid == guid).FirstOrDefault().Value,
VoteCount = gg.Count(),
}
).ToList();
Whats happening is the if a users vote does not exist for a TblPoll Question im getting empty array when the question should be returned even if there are not votes for that user.
So how would the linq query work for the reuslts to occur as i suggested?