简体   繁体   中英

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

I am having some issues getting my LINQ statement to work. 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. I'm not sure how to get the following into a LINQ expression

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

So far my LINQ is:

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.

 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:

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.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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