Linq to Entities:简单选择时出现 NullReferenceException

发布于 2024-09-30 03:38:34 字数 1872 浏览 2 评论 0原文

我有下面这句话:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

追星践月 2024-10-07 03:38:34

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

var customers = from customer in clientes
                select customer;

但是,由于您没有提供很多详细信息,我不能肯定地说这就是问题所在。

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:

var customers = from customer in clientes
                select customer;

But, since you haven't provided many details I cannot say that is the problem for sure.

淡笑忘祈一世凡恋 2024-10-07 03:38:34

在查看您的编辑后,我将发布另一个答案。

我看到的问题是,您假设从此返回的值:

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

将具有与 Context.ps_customer 中的一行或多行匹配的 id_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;

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:

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

is going to have a value for id_customer that matches one or more rows from Context.ps_customer, which is fallible because it's susceptible to delete anomolies (ie: if the order returned by FirstOrDefault() 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:

//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;
情仇皆在手 2024-10-07 03:38:34

编辑:
尝试此查询 -

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

如果 c 返回 Count=0,则意味着您的表没有任何条目。这就是为什么 null 问题......无论如何,我建议仔细调试......并确定它到底在哪里引发异常,有时当问题可能与其他问题有关时,您只是在查看错误的内容。

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

EDIT :
Try this query -

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

If c returns Count=0 it means your table didnt have any entries. So that is why the null 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 table ps_customer returns null 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

凉栀 2024-10-07 03:38:34

在我看来, 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 evaluate customers.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 with ToList().FirstOrDefault(), which says to the database "give me everything", and then selects the first record. Just a tip!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文