A Smarter Entity Framework Include Method

One of the things I have always disliked about Entity Framework and their support for LINQ is that while your whole LINQ statement is compile time checked to make sure you didn’t fat finger any SQL statement, the Include() statement is not. How many times has something like this happened to you? var account1001 = (new AccountEntities()) .Accounts.Include("Userx") .Where(account => account.Id == 1001) .SingleOrDefault(); You might have noticed in the above that I probably wanted “Users” instead of “Userx”. I don’t know about you but this happens to me all the time, and if the code was just compile time checks like the rest of the LINQ statement everything would be great and I would instantly catch the bug when the code was compiled. ...

February 27, 2011 · 2 min · 377 words · Nick Berardi

Interesting Extension Hack To Get Around NullReferenceException's

Today I came across an interesting extension pattern that I didn’t know how the runtime would react. Normally when you do something like the following: string s = null; Console.WriteLine(s.Trim()); // throws NullReferenceException You get a NullReferenceException meaning that you didn’t first check to see if the object was null before trying to call one of its methods. This is pretty common and results in patterns that usually look like this: ...

April 24, 2008 · 2 min · 313 words · Nick Berardi