This question is locked. New answers and comments are not allowed.
This has been driving me crazy and I now believe this is most likely a bug as I've never ran across it before.
The table ConsignmentAgreements has a field CANumber which is never null and is not a primary key. The second table, Orders, has a field PONumber which does allow null fields and is also not a primary key. I want to create a basic join that lists all the ConsignmentAgreements and the number of orders that have the CANumber as the PONumber. Very simple stuff. Wrong.
Here is my code:
Running that code gives me a "Object reference not set to an instance of an object." error. Meanwhile, running this exact same query using LINQPad it immediately gives me the desired result.
However, if I code to:
It works, but seems unnecessary.
Can someone explain that one to me?
The table ConsignmentAgreements has a field CANumber which is never null and is not a primary key. The second table, Orders, has a field PONumber which does allow null fields and is also not a primary key. I want to create a basic join that lists all the ConsignmentAgreements and the number of orders that have the CANumber as the PONumber. Very simple stuff. Wrong.
Here is my code:
var cas = (from ca
in
_ctx.ConsignmentAgreements
join o
in
_ctx.Orders on ca.CANumber equals o.PONumber into j1
from loj
in
j1.DefaultIfEmpty()
group loj by
new
{
ca.ConsignmentAgreementID,
ca.CANumber,
Location = ca.Location.Name,
ca.Descr
} into cag
select
new
{
ConsignmentAgreementID = cag.Key.ConsignmentAgreementID,
CANumber = cag.Key.CANumber,
Location = cag.Key.Location,
Descr = cag.Key.Descr,
NumberOfOrders = cag !=
null
? cag.Count() : 0
}).ToList();
Running that code gives me a "Object reference not set to an instance of an object." error. Meanwhile, running this exact same query using LINQPad it immediately gives me the desired result.
However, if I code to:
var cas = (from ca
in
_ctx.ConsignmentAgreements.ToList()
join o
in
_ctx.Orders.ToList() on ca.CANumber equals o.PONumber into j1
from loj
in
j1.DefaultIfEmpty()
group loj by
new
{
ca.ConsignmentAgreementID,
ca.CANumber,
Location = ca.Location.Name,
ca.Descr
} into cag
select
new
{
ConsignmentAgreementID = cag.Key.ConsignmentAgreementID,
CANumber = cag.Key.CANumber,
Location = cag.Key.Location,
Descr = cag.Key.Descr,
NumberOfOrders = cag !=
null
? cag.Count() : 0
}).ToList();
It works, but seems unnecessary.
Can someone explain that one to me?