Tip 40 – How to materialize presentation models via L2E

Problem: Imagine that you have these entities public class Product {     public int ID { get; set; }     public string Name { get; set; }     public virtual Category Category { get; set; } } public class Category {     public int ID { get; set; }     public string Name { get;…

5

Tip 37 – How to do a Conditional Include

Problem Someone asked how to do a Conditional Include a couple of days ago on StackOverflow. They wanted to query for some entity (lets say Movies) and eager load some related items (lets say Reviews) but only if the reviews match some criteria (i.e. Review.Stars == 5). Unfortunately though this isn’t strictly supported by EF’s…

13

Tip 35 – How to write OfTypeOnly<TEntity>()

If you write a LINQ to Entities query like this: var results = from c in ctx.Vehicles.OfType<Car>()               select c; It will bring back, Cars and any type that derives from Car, like say SportCar or SUV. If you just want Cars and you don’t want derived types like SportCars or SUVs in LINQ to…

4

Tip 31 – How to compose L2O and L2E queries

Imagine you want to write a query like this: var possibleBuyers=      from p in ctx.People      where p.Address.City == “Sammamish” && InMarketForAHouse(p)      select p; Now theoretically this is possible so long as there is a SQL translation for InMarketForAHouse.  In EF 4.0 you do this by creating a CLR stub for the required…

2

Tip 30 – How to use a custom database function

Imagine you have a database function like the DistanceBetween function in Nerd Dinner: CREATE FUNCTION [dbo].[DistanceBetween](   @Lat1 as real,   @Long1 as real,   @Lat2 as real,   @Long2 as real)RETURNS realASBEGIN…END And you want to use it with the Entity Framework. Declaring the Function The first step is to open your EDMX file in the XML editor…

11

Tip 28 – How to implement an Eager Loading strategy

Background: Over the last 2 years lots of people have complained about the way Eager loading works in the Entity Framework, or rather the way you ask the Entity Framework to eagerly load. Here is how you do it: var results = from b in ctx.Blogs.Include(“Posts”)               where b.Owner == “Alex”               select b; This…

20

Tip 21 – How to use the Single() operator – EF 4.0 only

This is 21st post in my ongoing series of Entity Framework Tips, and the first that is specific to EF 4.0. Entity Framework 4.0 Beta 1 As you’ve probably heard VS 2010 Beta 1 is now available to subscribers which means some of you can get your hands on EF 4.0 Beta 1 too. If…

3

Tip 11 – How to avoid Relationship Span

Background and Motivation: In my last post on EF Jargon I introduced the concept of Relationship Span. If you remember Relationship Span is basically responsible for compensating for the lack of Foreign Key properties* in your Entity. Relationship Span for an Entity, lets say StaffMember, insures that the Entity Framework knows the EntityKeys of the…

5

Tip 5 – How to restrict the types returned from an EF Query.

Imagine you have a model that looks like this:   How do you query for just Cars? This is where OfType<SubType>() comes in. You write something like this: var onlyCars = from car in ctx.Vehicles.OfType<Car>()                select car; And this works, fine. It restricts the results to just Cars, which incidentally includes Cars, SportsCars and…

9