【问题标题】:LINQ to SQL: Left join on multiple columnsLINQ to SQL:多列的左连接
【发布时间】:2016-07-18 08:17:08
【问题描述】:

首先,我搜索了 google/SO,查看了一些示例,但我没有设法编写正确的 linq 表达式:

这是我的工作 sql 查询的样子:

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

这是我对 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();

【问题讨论】:

标签: c# sql-server linq


【解决方案1】:

在您的查询中,您没有执行任何left join。 试试这个:

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 */
}

【讨论】:

  • 错误5 join 子句中的表达式类型不正确。调用“GroupJoin”时类型推断失败。
  • @Stefan - 这是因为 on 的匿名类型的类型/名称不匹配。立即尝试
  • 在 UserVoted = v.Vote : 错误 5 当前上下文中不存在名称“v”
  • @Stefan - 是的 - 我的错 - 它应该是 jointRecord.Vote。固定
  • 非常感谢吉拉德格林 :)
【解决方案2】:

您必须使用.DefaultIfEmpty() 来执行左连接。然后你需要决定在正确的表产生空值的情况下该怎么做。您可以为此使用三元运算符( ? : )

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;

【讨论】:

    【解决方案3】:

    如果您的问题是为这些多个连接条件分配关键字:

    // on new { p.Id, userId} equals new {v.PlaceId, v.UserId}
    

    试试

    on new { a = p.Id, b = userId} equals new { a = v.PlaceId, b = v.UserId}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-21
      • 2013-06-13
      • 1970-01-01
      • 2011-10-14
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多