NullReferencException in DLinq lambda method

A

Andrus

I tried DLinq code created by Marc and got NRE in ToList() line.
Any idea what causes this ?

Andrus.

Code:

Northwind db = CreateDB();
var orders = db.GetTable<Order>().ToArray().AsQueryable();

var query = from order in orders
select new
{
OrderID = order.OrderID,
Customer = new
{
ContactName = order.Customer.ContactName,
ContactTitle = order.Customer.ContactTitle
}
};
var list = query.ToList();

result:

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="Anonymously Hosted DynamicMethods Assembly"
StackTrace:
at lambda_method(ExecutionScope , Order )
at System.Linq.Enumerable.<SelectIterator>d__d`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

....
 
J

Jon Skeet [C# MVP]

I tried DLinq code created by Marc and got NRE in ToList() line.
Any idea what causes this ?

I suggest you find the query it's generated and look at the results.
Look for an order which doesn't have a customer, and check that the
query actually picks up the customer information to start with.

Jon
 
M

Marc Gravell

Jon's answer looks about right... as a quick check, try adding:

where order.Customer != null

between the "from" and "select"; if it goes away, that is the problem.

Marc
 
A

Andrus

Jon's answer looks about right... as a quick check, try adding:
where order.Customer != null

between the "from" and "select"; if it goes away, that is the problem.

I added this check and NRE disappears.
However query does not return any rows in this case.
Is this expected result from this query ?
Should it work without where clause also ?

Andrus.
 
J

Jon Skeet [C# MVP]

Andrus said:
I added this check and NRE disappears.
However query does not return any rows in this case.
Is this expected result from this query ?
Should it work without where clause also ?

Have you looked at the generated SQL?
 
F

Frans Bouma [C# MVP]

Andrus said:
I tried DLinq code created by Marc and got NRE in ToList() line.
Any idea what causes this ?

Andrus.

Code:

Northwind db = CreateDB();
var orders = db.GetTable<Order>().ToArray().AsQueryable();

var query = from order in orders
select new {
OrderID = order.OrderID,
Customer = new {
ContactName = order.Customer.ContactName,
ContactTitle = order.Customer.ContactTitle
}
};
var list = query.ToList();

result:

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="Anonymously Hosted DynamicMethods Assembly"
StackTrace:
at lambda_method(ExecutionScope , Order )
at System.Linq.Enumerable.<SelectIterator>d__d`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

ToList() enumerates the list, so it will crash if order.Customer is null.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
A

Andrus

ToList() enumerates the list, so it will crash if order.Customer is null.

new operator in query creates new object. so order.Customer *cannot* be
null.

Andrus.
 
M

Marc Gravell

new operator in query creates new object. so order.Customer *cannot*

No - the "new Customer" in the query posted only applies to the
anonymous type you are generating. It says nothing at all about whether
order.Customer is null or not.

Try simply enumerating the orders, and listing what Customer is...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top