简体   繁体   中英

LINQ DefaultIfEmpty issue on object with non-nullable value type property

Requirement

  1. I need to join Table1 and Table2
  2. The key between two tables are ID which is System.Guid type and is non-nullable value type
  3. If Table2.ID is null, I need to get null record from Table1.

LINQ syntax I wrote is as follows.

from records in DBContext.Table1
join history in DBContext.Table2 into recordhistory
from records in recordhistory.DefaultIfEmpty()
select (n => n);

The error I got is "The null value cannot be assigned to a member with type System.Guid which is a non-nullable value type."

Can someone advise me on this? Thank you very much.

Assuming you have an ID property, following should work as inner join:

var result = DBContext.Table1
              .Join(DBContext.Table2, t1 => t1.ID, t2 => t2.ID, (t1, t2) => t1);

I guess, the query, you provided, shoulg give an error, saying that on statement should be specified. So, I guess it should look somewhat like this:

from records in DBContext.Table1
join history in DBContext.Table2 on records.ID equals history.ID into temp
from recordhistory in temp.DefaultIfEmpty()
select new { Record = records, History = recordhistory };

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