How to avoid Query Plan re-compilation when using IEnumerable.Contains in Entity Framework LINQ queries?

This is a great question. First of all, here are a couple of workarounds that come to mind (they all require changes to the query): First workaround This one maybe a bit obvious and unfortunately not generally applicable: If the selection of items you would need to pass over to Enumerable.Contains already exists in a … Read more

Help with Linq and Generics. Using GetValue inside a Query

You need to convert the code to an expression tree. using System; using System.Linq; using System.Linq.Expressions; using System.Reflection; namespace WindowsFormsApplication1 { static class Program { [STAThread] static void Main() { using (var context = new NorthwindEntities()) { IQueryable<Customer> query = context.Customers; query = Simplified<Customer>(query, “CustomerID”, “ALFKI”); var list = query.ToList(); } } static IQueryable<T> Simplified<T>(IQueryable<T> … Read more

EF4 LINQ Ordering Parent and all child collections with Eager Loading (.Include())

If you need to have Ordering or Filtering on inner navigation properties (e.g. Models) then you cannot eager load them using Include method anymore. Instead, you can use EntityCollection<TEntity>.CreateSourceQuery Method like this: List years = db.Years.OrderBy(“it.Name”).ToList(); foreach(year in years) { var makesQuery = year.Makes.CreateSourceQuery().OrderBy(m => m.Name); year.Makes.Attach(makesQuery); foreach(make in year.Makes) { var modelsQuery = make.Models.CreateSourceQuery().OrderBy(m … Read more

LINQ To Entities + Include + Anonymous type issue

As Ladislav mentioned, Include only works if you select the Ticket entity directly. Since you’re projecting other information out, the Include gets ignored. This should provide a good work-around: var data = ctx.Set<Ticket>() .Select(p => new { Ticket = p, Clients = p.Client, LastReplyDate = p.Replies.Max(q => q.DateCreated) }); First of all, each Ticket’s Clients … Read more

Linq to Entities : using ToLower() on NText fields

Never use .ToLower() to perform a case-insensitive comparison. Here’s why: It’s possibly wrong (your client collation could be, say, Turkish, and your DB collation not). It’s highly inefficient; the SQL Emitted is LOWER instead of = with a case-insensitive collation. Instead, use StringComparison.OrdinalIgnoreCase or StringComparison.CurrentCultureIgnoreCase: var q = from f in Context.Foos where f.Bar.Equals(“hi”, StringComparison.OrdinalIgnoreCase) … Read more

How do you construct a LINQ to Entities query to load child objects directly, instead of calling a Reference property or Load()

You want to use the .Include(string) method references in this “Shaping query results” article. var item = from InventoryItem item in db.Inventory.Include(“ItemTypeReference”).Include(“OrderLineItems”) where item.ID == id select item; There is probably a “sql” style syntax for the Includes as well. Also see this article about moving from LINQ-to-SQL to LINQ-to-Entities. For others looking for a … Read more