代码之家  ›  专栏  ›  技术社区  ›  AaronLS

使用文本值的LINQ连接

  •  0
  • AaronLS  · 技术社区  · 15 年前

    这是一种假设,因为我正在考虑如何设计这个。请考虑在联接条件中包含一些文字整数值的查询:

    Select * 
    From LeftPeople l
    Inner Join RightPeople r On r.RecordId = l.RecordId and r.ResultId = 3 and l.ResultId = 7
    

     var query = from leftPerson in LeftPeople
       join rightPerson in RightPeople on 
       new { RecordId = leftPerson.RecordId, RightResultId = 3,  LeftResultId = leftPerson.ResultId }
       equals new { RecordId = rightPerson.recordid, RightResultId = rightPerson.ResultId ,  LeftResultId = 7 }
       select new { LeftPerson = leftPerson, RightPerson = rightPerson };
    
    3 回复  |  直到 15 年前
        1
  •  3
  •   Jon Skeet    15 年前

    如果只需要RightResultId为3的项,则使用Where子句筛选其他项。我不认为这真的属于连接子句。可能有用,但我觉得很乱。

    var query = from left in LeftPeople
                where left.RightResultId = 3
                join right in RightPeople.Where(r => r.LeftResultId = 7)
                on left.RecordId equals right.RecordId
                select new { LeftPerson = left, RightPerson = right };
    

    那是假设 RecordId

        2
  •  1
  •   Justin Niessner    15 年前

    LeftPeople.ResultId和RightPeople.ResultId的筛选器属于LINQ语句中的Where子句:

    var query = from l in LefPeople
                join r in RightPeople on l.RecordId equals r.ResultId
                where r.ResultId == 3 && l.ResultId == 7
                select new { LeftPerson = l, RightPerson = r };
    

    也就是说,我可能认为SQL查询的WHERE子句中也应该包含这些过滤器。

        3
  •  1
  •   Amy B    15 年前

    如果你建立了联系,你可以写:

    from left in leftPeople
    where left.ResultId == 7
    from right in left.RightPeople
    where right.ResultId == 3
    select new {left, right};