Categories

Linq NullReferenceException on foreign key

I used Linq first time in my life this week – wow! It creates some trouble for my brain to write lambda expressions but I am getting the hang of it.

I noticed one odd thing so far. Using Linq to SQL to map to a table with a nullable foreign key column gives you a NullReferenceException when accessing a null tuple through the Association property. Maybe that is logical. To circumvent the issue I check the foreign key field for null before accessing the Association property:

….Where(x => x.SomeId != null).Select(x => x.Options);

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>