简体   繁体   English

您如何使用 linq 离开加入“非空”?

[英]How do you Left join 'Is Not Null' using linq?

I am having some issues getting my LINQ statement to work.我在让我的 LINQ 语句正常工作时遇到一些问题。 I am left joining a table, secondTable, where one of the columns can be null but I only need the records where this column is not null.我要加入一个表 secondTable,其中一列可以是 null,但我只需要该列不是 null 的记录。 I'm not sure how to get the following into a LINQ expression我不确定如何将以下内容放入 LINQ 表达式

LEFT JOIN secondTable b ON a.ID = b.oneTableID AND b.name IS NOT NULL

So far my LINQ is:到目前为止,我的 LINQ 是:

var list = await (from one in dbRepository.oneTable
                  join two in dbRepository.secondTable
                  on new { name = one.name, phone = one.phone, height = { is not null} } equals new 
                  { name = two.name, phone = two.phone, height = two.height 
                  into temp
                  from two in temp.DefaultIfEmpty()
                  select new.....

Any Ideas?有任何想法吗?

EDIT 1: I was able to find a solution.编辑1:我能够找到解决方案。

 var list = await (from one in dbRepository.oneTable
                  join two in dbRepository.secondTable
                  on new { name = one.name, phone = one.phone, height = false } equals new 
                  { name = two.name, phone = two.phone, height = string.IsNullOrEmpty(two.height)}
                  into temp
                  from two in temp.DefaultIfEmpty()
                  select new.....

You have to use SelectMany possibility to create LEFT JOIN:您必须使用SelectMany来创建 LEFT JOIN:

var query = 
    from one in dbRepository.oneTable
    from two in dbRepository.secondTable
        .Where(two => two.name = one.name && two.phone == one.phone 
            && two.height != null)
        .DefaultIfEmpty()
    select new.....

Try this one:试试这个:

var list = await (from one in dbRepository.oneTable
                  join two in dbRepository.secondTable
                  on new { name = one.name, phone = one.phone} 
                  equals new 
                  { name = two.name, phone = two.phone}
                  into temp
                  from two in temp.DefaultIfEmpty()
                  where one.height == null || one.height = two.height 
                  select new.....

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

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