【问题标题】:Linq to Entities: NullReferenceException on a simple selectLinq to Entities:简单选择上的 NullReferenceException
【发布时间】:2011-05-01 10:30:30
【问题描述】:

我有以下一句话:

var customers = from customer in Context.ps_customer
                select customer;

如你所见,这是世界上最简单的一句话。好吧,它抛出了 NullReferenceException,我不知道为什么。实际上,异常是在

处抛出的
List<ps_customer> clientes = customers.ToList<ps_customer>();

但如果我在 Linq 语句中设置断点并尝试查看客户价值,则会出现 NullReferenceException。

有人知道我为什么会得到这个异常吗?

编辑:我将提供更多信息:

MyEntityModel Context = new MyEntityModel();

var solicitudes = from  solicitud in Context.ps_orders
                  where solicitud.date_add.Year == fecha.Year &&
                        solicitud.date_add.Month == fecha.Month &&
                        solicitud.date_add.Day == fecha.Day
                  select solicitud;

//This return correct data
ps_orders orden = solicitudes.ToList<ps_orders>().FirstOrDefault(); 

var customers = from customer in Context.ps_customer
                where customer.id_customer == orden.id_customer
                select customer;

var orden_detalles = from oDetalle in Context.ps_order_detail
                     where oDetalle.id_order == orden.id_order
                     select oDetalle;

var direcciones = from oDireccion in Context.ps_address
                  where oDireccion.id_address == orden.id_address_delivery
                  select oDireccion;

ps_address direccion = direcciones.FirstOrDefault(); //Correct data
List<ps_order_detail> detalles = orden_detalles.ToList<ps_order_detail>(); //Correct data
ps_customer clientes = customers.FirstOrDefault(); //NullReferenceException

我完全确定 ps_customer 有数据,具体是 2 行,我已经从 .edmx 中删除了 ps_customer 实体,然后我再次添加它,它仍然会发生

非常感谢!

编辑 2: 我已经复制了表的create语句,创建了一个名为customerTwo的新表,插入了新数据,它仍然失败......顺便说一句,我使用的是MySQL,数据库是由Prestashop创建的,以防万一信息很有用...

【问题讨论】:

    标签: linq-to-entities nullreferenceexception


    【解决方案1】:

    在我看来ps_orders orden = solicitudes.ToList&lt;ps_orders&gt;().FirstOrDefault(); 正在返回null。由于延迟评估,您在尝试评估 customers.FirstOrDefault() 之前不会看到它。我建议验证是否正确定义了关怀。

    此外,您可能需要考虑通过尽可能合并查询来减少数据库流量 - FirstOrDefault() 可以直接应用于实体查询,这通常转换为 SELECT TOP 1 查询。将此与ToList().FirstOrDefault() 进行对比,后者对数据库说“给我一切”,然后选择第一条记录。只是一个提示!

    【讨论】:

    • 恐怕你错了。我已经检查过了,“orden”不为空。我什至可以告诉你 orden.id_orden 的值为 2。
    【解决方案2】:

    编辑: 试试这个查询 -

    var customers = (from c in Context.ps_customer
                    select c).ToList();
    

    如果c 返回 Count=0,则表示您的表没有任何条目。所以这就是null 问题的原因......无论如何,我建议仔细调试......并确定它究竟在哪里引发了异常,有时你只是在看错误的东西,而问题可能与其他东西有关。

    原文: customers 有空值..显然意味着由于某种原因你的表ps_customer 返回null 你确定它有数据并且模型是正确的......也可能值得看看你的代码......我的猜测是检查是否你的上下文为空

    【讨论】:

    • 我确定该表有数据,具体两行。上下文不为空,我已经检查过了。事实上,我还有另外两个查询,它们都返回了一些东西,所以由于某种原因,唯一错误的表是 ps_customer...
    【解决方案3】:

    看了你的编辑后,我将发布另一个答案。

    我看到的问题是你假设从这里返回的值:

    ps_orders orden = solicitudes.ToList<ps_orders>().FirstOrDefault(); 
    

    id_customer 的值将与来自Context.ps_customer 的一个或多个行匹配,这是错误的,因为它容易被删除异常(即:如果FirstOrDefault() 返回的订单指的是具有已从数据库中删除,那么您的客户查询将不会返回任何结果)。

    我建议使用另一种查询方法,也许加入两个表,而不是尝试匹配 id_customer 的特定值,这样说:

    //disclaimer: I'm primarily a SQL dev, not LINQ or EF, so this syntax may be wrong
    var customers = from customer in Context.ps_customer
    join orden in solicitudes
    on customer.id_customer == orden.id_customer
    select customer;
    

    【讨论】:

    • 我也检查过了,确定有一行具有正确的 id_customer。此外,为了简化查询,我尝试了最简单的查询,即没有“where”,正如您在我的第一篇文章中看到的那样,它仍然会崩溃。
    • 好吧,如果一切都是非空的,你就不会收到这个错误......你是否尝试过设置断点并在调试模式下运行它以隔离为空的数据成员,然后从那里?作为记录,我仍然认为 Context.ps_customer 为空......
    • 是的,我尝试在调试模式下运行它,但问题是 Linq 使用延迟操作,所以直到我执行 customers.FirstOrDefault() 的那一刻,它才执行代码,我不知道具体问题,因为它只是在那一刻失败,错误只是 NullReferenceException...
    • 您是否正在查看堆栈跟踪和/或检查内部异常?
    【解决方案4】:

    您收到此异常是因为在评估您的 LINQ 查询时Context.ps_customer 为空。根据您的第二行代码,您的查询应该如下所示:

    var customers = from customer in clientes
                    select customer;
    

    但是,由于您没有提供很多细节,我不能肯定这是问题所在。

    【讨论】:

    • 不可能是那句话,因为“clientes”是在查询之后定义的,它是一个存储查询结果的变量。顺便说一句,我将编辑问题以包含更多信息。感谢回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多