【问题标题】:Linq query NullReferenceException on multiple (cascade) left joins多个(级联)左连接上的 Linq 查询 NullReferenceException
【发布时间】:2014-10-29 15:32:48
【问题描述】:

我正在使用 Linq 查询来获取客户及其可选的主要地址(客户可以有零个或多个地址)。 对象层次结构如下:

  • 客户
    • CustomerAddress(包含布尔属性 Main)
      • 地址

这是我正在使用的查询:

var qry = from customer cus in GetDBContext(c).customer
    join cusadd in GetDBContext(c).customeraddress on new { cus_code = cus.cus_code, main = "1" } equals new { cus_code = cusadd.cus_code, main = cusadd.Main_addr } into grpcusadd
    from cusadd in grpcusadd.DefaultIfEmpty()
    join add in GetDBContext(c).address on new { addr_code = cusadd.Addr_Code } equals new { addr_code = add.Addr_Code } into grpadd
    from add in grpadd.DefaultIfEmpty()
    select new { cus, cusadd, add };

var customers = qry.ToList();

当我在数据库上(通过 EF)执行它时,它会正确返回值。 当我在内存对象的模拟上下文中执行它时,我得到一个 NullReferenceException: Object reference not set to an instance of an object。

我能够通过检查第二个左连接中的空值来修复此错误,因为第一个左连接返回空值:

join add in GetDBContext(c).address on new { addr_code = cusadd == null ? null : cusadd.Addr_Code } equals new { addr_code = add.Addr_Code } into grpadd

我找到了一个结论相同但没有解释的博文:http://technologycraftsmen.net/blog/2010/04/14/multiple-outer-joins-in-linq-to-sql/

为什么这个查询在本地对象而不是数据库上失败?

在 Linq 中,级联左外连接是否应该总是这样写?

感谢您的反馈!

【问题讨论】:

    标签: c# linq entity-framework


    【解决方案1】:

    Linq 很棒,但没有任何抽象是完美的。这是底层抽象有点泄露的情况。

    当使用真实上下文执行表达式时,它会使用 LEFT JOIN 转换为 Transact SQL 语句。表达式从未在 CLR 中实际执行,一切都发生在数据库中。即使正确的表中没有匹配的记录,查询也会成功。

    当针对您的模拟上下文执行查询时,实际的查询执行发生在 CLR 而不是数据库中。此时,表达式的运行就像您编写了非 LINQ C# 代码一样。这意味着针对 null 对象的属性的测试将抛出您所看到的 NullReferenceException。

    想象一下如果这只是两个序列之间的连接会发生什么可能会有所帮助,例如:

    var customers = new List<Customer> { new Customer { Id = 1, Name = "HasAddress" }, new Customer { Id = 2, Name = "HasNoAddress" } };
    
    var addresses = new List<Address> { new Address { Id = 1, CustomerId = 1, Street = "123 Conselyea Street" } };
    
    
    var customerAddresses = from Customer cus in customers
                            join address in addresses on cus.Id equals address.CustomerId into grouped
                            from ca in grouped.DefaultIfEmpty()
                            select new { CustomerId = cus.Id, Name = cus.Name, Street = ca.Street };
    

    赋值“Street = ca.Street”将抛出 NullReferenceException,因为我们的第二个客户没有匹配的地址。我们将如何解决这个问题?使用三元运算符,就像您在级联连接的修复中包含的那样:

    var customerAddresses = from Customer cus in customers
                            join address in addresses on cus.Id equals address.CustomerId into grouped
                            from ca in grouped.DefaultIfEmpty()
                            select new { CustomerId = cus.Id, Name = cus.Name, Street = (ca != null) ? ca.Street : null };
    

    在模拟上下文的情况下,您的级联联接不是 Transact SQL 联接,它只是对象和序列的正常 C# 用法,如我的示例所示。

    我不知道有什么方法可以编写代码来忽略在 CLR 中执行和在数据库中执行之间的规则差异。通过为 DefaultIfEmpty 方法调用提供默认实例可能会做一些棘手的事情,但这对我来说似乎很棘手。

    希望一旦我们在 C# 中使用空传播运算符,这将得到改进。

    【讨论】:

    • 感谢您的回答!结论:如果 Linq 查询将在数据库和 CLR 中执行,则查询应该处理这两种情况的问题。 null 传播运算符确实可以提供更简洁的代码。
    • 很好的答案,我自己在测试中遇到了这个问题,给出了内存表示。困难在于确保您不使用无法转换为数据库的方法或运算符,否则它将最终从数据库中抛出或返回更多,然后运行 ​​CLR 代码以进一步过滤。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 2015-02-05
    • 2018-11-10
    相关资源
    最近更新 更多