简体   繁体   English

LINQ左外连接排除

[英]LINQ left outer join with exclusion

I'm trying to exclude the results of a first query from being included in the results of my second query. 我试图排除第一个查询的结果被包含在我的第二个查询的结果中。

The SQL equivalent is here, where A is my current query, and B is the old query. SQL等效于此处,其中A是我当前的查询,B是旧查询。

在此输入图像描述

I've been trying to use this guide to do the left join, but I can't seem to figure out how it should work in my case. 我一直在尝试使用本指南进行左连接,但我似乎无法弄清楚它在我的情况下应该如何工作。 I'm just not understanding how this should work (I can't get the syntax highlighting to be happy). 我只是不明白这应该如何工作(我不能让语法突出显示为快乐)。

var emp = from employee in empl
    join jc in jce on callout.job_class_code_fk equals jc.job_class_code_fk
    join av in ab on employee.employee_id_pk equals av.employee_id_fk
    join sh in sho on employee.employee_id_pk equals sh.employee_id_fk into lj
    from rnd2 in lj.DefaultIfEmpty()
    orderby employee.seniority descending
    select new
    {
      eid = employee.employee_id_pk,
      sen = employee.seniority,
      nam = employee.employee_name,
      pho = employee.phone_number,
      lje = sho == null ? sho.employee_id_fk : null //left outer join with exclusion??
    };

EDIT:: Based on the suggestions in the comments I've tried both of the following. 编辑::根据评论中的建议,我尝试了以下两种方法。 While I don't have syntax issues any more, neither of the following return ANY results, so there's still something wrong here. 虽然我不再有语法问题,但以下都没有返回任何结果,所以这里仍然有问题。

    var emp = from employee in empl
              join jc in jce on callout.job_class_code_fk equals jc.job_class_code_fk
              join av in ab on employee.employee_id_pk equals av.employee_id_fk
              join sh in sho on employee.employee_id_pk equals sh.employee_id_fk into lj
              from rnd2 in lj.DefaultIfEmpty() where sho == null
              orderby employee.seniority descending
              select new
              {
                  eid = employee.employee_id_pk,
                  sen = employee.seniority,
                  nam = employee.employee_name,
                  pho = employee.phone_number,
              };

    var emp = from employee in empl
              join jc in jce on callout.job_class_code_fk equals jc.job_class_code_fk
              join av in ab on employee.employee_id_pk equals av.employee_id_fk
              join sh in sho on employee.employee_id_pk equals sh.employee_id_fk into lj
              from rnd2 in lj.DefaultIfEmpty() where rnd2 == null
              orderby employee.seniority descending
              select new
              {
                  eid = employee.employee_id_pk,
                  sen = employee.seniority,
                  nam = employee.employee_name,
                  pho = employee.phone_number,
              };

Okay so (to me anyway) the answer that is the easiest to read and understand ended up being this. 好吧(对我而言)最容易阅读和理解的答案最终成为了这个。

Create two lists, the one I want to exclude, and the master list. 创建两个列表,我要排除的列表和主列表。

They we run master.Except(exclude) and voila. 他们我们运行master.Except(排除)和瞧。 We've accomplished the effect of a left outer join with exclusion. 我们已经完成了左外连接的效果和排除。

Here's the working code. 这是工作代码。 The solutions above could very well have worked, as there was another problem with how the first list was being put together. 上面的解决方案很可能有效,因为第一个列表的组合方式存在另一个问题。

            var ex = from employee in empl
                 join sh in sho on employee.employee_id_pk equals sh.employee_id_fk
                 select new
                 {
                     eid = employee.employee_id_pk,
                     sen = employee.seniority,
                     nam = employee.employee_name,
                     pho = employee.phone_number,
                 };
        ex.Distinct();

        //get a list of employees who have the enabled orientations and job classifications
        var emp = from employee in empl
                  join jc in jce on employee.employee_id_pk equals jc.employee_id_fk
                  join av in ab on employee.employee_id_pk equals av.employee_id_fk
                  orderby employee.seniority descending
                  select new
                  {
                      eid = employee.employee_id_pk,
                      sen = employee.seniority,
                      nam = employee.employee_name,
                      pho = employee.phone_number,
                  };
        emp = emp.Distinct();
        emp = emp.Except(ex);

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

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