Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to SQL: Left join on multiple columns

First of all, I have searched google/SO, checked some examples, but I didn't manage to write the proper linq expression:

This is how my working sql query looks like:

select *
from Places p
left join VoteLog v
on p.Id = v.PlaceId
and v.UserId = '076a11b9-6b14-4230-99fe-28aab078cefb' --demo userid

This is my attempt with linq:

public IQueryable<Place> GetAllPublic(string userId)
{
    var result = (from p in _db.Places
                 join v in _db.VoteLogs
                 on p.Id equals v.PlaceId // This works but doesn't fully reproduce my SQL query
                 // on new { p.Id, userId} equals new {v.PlaceId, v.UserId} -> Not ok
                 where p.Public == 1
                 select new
                 {
                    Id = p.Id,
                    UserId = p.UserId,
                    X = p.X,
                    Y = p.Y,
                    Titlu = p.Titlu,
                    Descriere = p.Descriere,
                    Public = p.Public,
                    Votes = p.Votes,
                    DateCreated = p.DateCreated,
                    DateOccured = p.DateOccured,
                    UserVoted = v.Vote
                 })
        .ToList()
        .Select(x => new Place()
        {
            Id = x.Id,
            UserId = x.UserId,
            X = x.X,
            Y = x.Y,
            Titlu = x.Titlu,
            Descriere = x.Descriere,
            Public = x.Public,
            Votes = x.Votes,
            DateCreated = x.DateCreated,
            DateOccured = x.DateOccured,
            UserVoted = x.UserVoted
        }).AsQueryable();
like image 354
Gerald Hughes Avatar asked Jul 18 '16 08:07

Gerald Hughes


2 Answers

In your query you didn't do any left join. Try this:

from p in _db.places
join v in _db.VoteLogs

//This is how you join by multiple values
on new { Id = p.Id, UserID = userId } equals new { Id = v.PlaceId, UserID = v.UserID } 
into jointData

//This is how you actually turn the join into a left-join
from jointRecord in jointData.DefaultIfEmpty()

where p.Public == 1
select new
{
    Id = p.Id,
    UserId = p.UserId,
    X = p.X,
    Y = p.Y,
    Titlu = p.Titlu,
    Descriere = p.Descriere,
    Public = p.Public,
    Votes = p.Votes,
    DateCreated = p.DateCreated,
    DateOccured = p.DateOccured,
    UserVoted = jointRecord.Vote 
    /* The row above will fail with a null reference if there is no record due to the left join. Do one of these:
       UserVoted = jointRecord ?.Vote - will give the default behavior for the type of Uservoted
       UserVoted = jointRecord == null ? string.Empty : jointRecord.Vote */
}
like image 143
Gilad Green Avatar answered Oct 03 '22 22:10

Gilad Green


You have to use .DefaultIfEmpty() to perform a left join. Then you need to decide what to do in case the right table produces null. You can use a ternary operator ( ? : ) for that.

var result = 
    (from p in _db.Places
    join v in _db.VoteLogs
    on new { p.Id, userId } equals new { v.PlaceId, v.UserId } into LEFTJOIN
    from result in LEFTJOIN.DefaultIfEmpty()
    where p.Public == 1
    select new
    {
        Id = p.Id,
        UserId = p.UserId,
        X = p.X,
        Y = p.Y,
        Titlu = p.Titlu,
        Descriere = p.Descriere,
        Public = p.Public,
        Votes = p.Votes,
        DateCreated = p.DateCreated,
        DateOccured = p.DateOccured,
        UserVoted = result == null ? null /* replace with your value */ : x.Vote
    }).AsQueryable();

return result;
like image 26
Zein Makki Avatar answered Oct 03 '22 23:10

Zein Makki