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

This site uses Akismet to reduce spam. Learn how your comment data is processed.