Linq to Entities:简单选择时出现 NullReferenceException
我有下面这句话:
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,DataBase是由Prestashop创建的,以防万一信息很有用...
I have the following sentence:
var customers = from customer in Context.ps_customer
select customer;
As you can see, it is the most simple sentence in the world. Well, it throws a NullReferenceException, and I don't have any idea why. in fact, the exception is thrown at
List<ps_customer> clientes = customers.ToList<ps_customer>();
but if I set a breakpoint in the Linq sentence and try to see the customers value, I have the NullReferenceException.
Does anyone have any idea why I get this exception?
EDIT: I am going to provide a bit more information:
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
I am totally sure that ps_customer has data, 2 rows to be specific, and I have deleted the ps_customer entity from the .edmx and I have added it again, an it still happens
Thank you very much!
EDIT 2:
I have copied the create statement of the table, created a new table called customerTwo, inserted new data, and it still fails... By the way, I am using MySQL, and the DataBase is created by Prestashop, just in case that information is useful...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您收到此异常是因为在评估 LINQ 查询时
Context.ps_customer
为 null。根据您的第二行代码,您的查询应该如下所示:但是,由于您没有提供很多详细信息,我不能肯定地说这就是问题所在。
You're getting this exception because
Context.ps_customer
is null when your LINQ query is evaluated. Based on your second line of code it looks like your query should be the following:But, since you haven't provided many details I cannot say that is the problem for sure.
在查看您的编辑后,我将发布另一个答案。
我看到的问题是,您假设从此返回的值:
将具有与 Context.ps_customer 中的一行或多行匹配的 id_customer 值>,这是错误的,因为它容易出现删除异常(即:如果
FirstOrDefault()
返回的订单引用已从数据库中删除的客户,那么您的客户查询将不会返回任何结果) 。我建议使用另一种查询方法,也许连接两个表,而不是尝试匹配 id_customer 的特定值,如下所示:
I'm going to post another answer after looking at your edit.
The problem as I see it is that you're assuming that the value returned from this:
is going to have a value for
id_customer
that matches one or more rows fromContext.ps_customer
, which is fallible because it's susceptible to delete anomolies (ie: if the order returned byFirstOrDefault()
refers to a customer that has been deleted from the database, then your customer query will return no results).I'd recommend using another query approach, perhaps joining on the two tables rather than trying to match against a specific value for
id_customer
, say something like this:编辑:
尝试此查询 -
如果
c
返回 Count=0,则意味着您的表没有任何条目。这就是为什么null
问题......无论如何,我建议仔细调试......并确定它到底在哪里引发异常,有时当问题可能与其他问题有关时,您只是在查看错误的内容。原文:
customers
有 null..显然意味着由于某种原因您的表ps_customer
返回null
您确定它有数据并且模型是正确的...还有可能值得查看您的代码..我的猜测是检查您的 Context 是否为 nullEDIT :
Try this query -
If
c
returns Count=0 it means your table didnt have any entries. So that is why thenull
issues...anyways I would recommend debugging carefully..and determining where exactly it throws the exception sometimes you are just looking at the wrong thing when the problem maybe with something else.Original :
customers
has null..obviously means for some reason your tableps_customer
returnsnull
are you sure it has data and model is right...also it might be worth looking at your code..my guess is check if your Context is null在我看来,
ps_orders orden = solicitudes.ToList().FirstOrDefault();
返回
null
。由于延迟评估,直到您尝试评估customers.FirstOrDefault()
时才会看到它。我建议验证关心的定义是否正确。此外,您可能需要考虑通过合并查询来减少数据库流量,您可以将
FirstOrDefault()
直接应用于实体查询,这通常会转换为 SELECT TOP 1 查询。将此与ToList().FirstOrDefault()
进行对比,后者对数据库说“给我一切”,然后选择第一条记录。只是一个提示!It looks to me that
ps_orders orden = solicitudes.ToList<ps_orders>().FirstOrDefault();
is returning
null
. You aren't seeing it until you attempt to evaluatecustomers.FirstOrDefault()
due to deferred evaluation. I'd suggest verifying that solicitudes is correctly defined.Additionally, you may want to consider reducing the amount of database traffic by consolidating queries where you can -
FirstOrDefault()
can be applied to an entity query directly, which usually translates to a SELECT TOP 1 query. Contrast this withToList().FirstOrDefault()
, which says to the database "give me everything", and then selects the first record. Just a tip!