tags

Expression

[Entity Framework] Using Include with lambda expressions

I’m currently working on a project that uses Entity Framework 4. Even though lazy loading is enabled, I often use the ObjectQuery.Include method to eagerly load associated entities, in order to avoid database roundtrips when I access them: var query = from ord in db.Orders.Include("OrderDetails") where ord.Date >= DateTime.Today select ord; Or if I also want to eagerly load the product: var query = from ord in db.Orders.Include("OrderDetails.Product") where ord.Date >= DateTime.

Automating null checks with Linq expressions

The problem Have you ever written code like the following ? X xx = GetX(); string name = "Default"; if (xx != null && xx.Foo != null && xx.Foo.Bar != null && xx.Foo.Bar.Baz != null) { name = xx.Foo.Bar.Baz.Name; } I bet you have ! You just need to get the value of xx.Foo.Bar.Baz.Name, but you have to test every intermediate object to ensure that it’s not null. It can quickly become annoying if the property you need is nested in a deep object graph….