简体   繁体   English

LINQ 查询中左外连接子句的多个条件

[英]Multiple conditions on a left outer join clause in a LINQ query

I would like to write the following SQL query with LINQ syntax to understand the fundamentals of LINQ queries.我想用 LINQ 语法编写以下 SQL 查询,以了解 LINQ 查询的基础知识。

SELECT q.*, qpph.*
  FROM [Questions] AS q
  LEFT OUTER JOIN [QuestionPoolPickHandles] AS qpph
    ON qpph.QuestionId = q.Id AND qpph.PickerId = 100
  WHERE qpph.Id IS NULL;

How can I apply left outer join and a condition in its ON clause at the same time using LINQ syntax?如何使用 LINQ 语法在其ON子句中同时应用左外连接和条件? From my readings, it seems it is likely to be not possible.从我的阅读来看,这似乎是不可能的。

Here's what I've tried so far.这是我迄今为止尝试过的。

var result = from q in context.Questions
             join qpph in context.PoolPickHandles
               on q.Id equals qpph.PickerId into Handles  // notice the 'qpph.PickerId = 100' is absent
             from m in Handles.DefaultIfEmpty()
             where m == null
             select q;

Any further elaboration will be appreciated.任何进一步的阐述将不胜感激。

With the help of the comments on the question, I've figured out writing the query using LINQ syntax.在对问题的评论的帮助下,我想出了使用 LINQ 语法编写查询的方法。

from q in context.Questions
join qpph in context.PoolPickHandles
  on new { questionId = q.Id, pickerId = user.Id } 
    equals new { questionId = qpph.QuestionId, pickerId = qpph.PickerId } 
      into Handles
from m in Handles.DefaultIfEmpty()
where m == null
select q;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM