简体   繁体   English

具有非空值类型属性的对象上的LINQ DefaultIfEmpty问题

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

Requirement 需求

  1. I need to join Table1 and Table2 我需要加入Table1和Table2
  2. The key between two tables are ID which is System.Guid type and is non-nullable value type 两个表之间的键是ID,它是System.Guid类型,并且是非空值类型
  3. If Table2.ID is null, I need to get null record from Table1. 如果Table2.ID为空,则需要从Table1获取空记录。

LINQ syntax I wrote is as follows. 我写的LINQ语法如下。

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." 我得到的错误是“不能将空值分配给类型为System.Guid的成员,这是非空值类型。”

Can someone advise me on this? 有人可以建议我吗? Thank you very much. 非常感谢你。

Assuming you have an ID property, following should work as inner join: 假设您具有ID属性,则以下内容应作为内部联接工作:

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. 我猜想,您提供的查询应该给出一个错误,说应该指定on语句。 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 };

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

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