Skip to content

Linq projection - unfriendly error message #1472

Open
@andyci

Description

@andyci

Example entity:

public class Customer {
    public virtual Contact BillingContact { get; set; }
}

public class Contact {
    public virtual DateTime DateOfBirth { get; set; }
}

(from c in Session.Query<Customer>()
select new {
DateOfBirth = c.BillingContact.DateOfBirth
}).ToList();

Because LINQ sees the projected property as being of type DateTime, if the BillingContact property is null then we get a NullReferenceException which tells us nothing about which property is actually causing the problem. When you have 20+ fields in your projection then it is extremely painful to track down which of the properties is causing this error. I know we could just do (DateTime?)c.BillingContact.DateOfBirth but this isn't always practical and doesn't help, e.g. in the case where a complex property that was originally non-nullable later becomes nullable.

I'm thinking of making a change to the codebase to perhaps include the property name that is failing in the exception details. Does this seem like a reasonable change to make? I can see there will be a bit of work required since, at the moment, only the column index is accessible from the method that is throwing.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions