LINQ to Entities
EDM primitive types
IEntity interface
casting types
.NET Framework

LINQ to Entities only supports casting EDM primitive or enumeration types with IEntity interface

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

This LINQ to Entities error usually appears when a query tries to cast an entity to a custom interface or CLR type that Entity Framework cannot translate into SQL. The message mentions EDM primitive or enumeration types because those are the kinds of casts EF knows how to map into the underlying query language, while interface casts such as IEntity are .NET concepts with no direct SQL translation.

Why the Cast Fails

Entity Framework does not execute your query immediately. It first translates the expression tree into SQL. If the query contains something like this:

csharp
var query = db.People
    .Where(p => ((IEntity)p).Id > 10);

EF must translate the cast to IEntity into SQL, and it cannot. Interfaces are not part of the EDM type system used by the query provider, so the translation fails before the query ever reaches the database.

What EF Can Translate

LINQ to Entities can usually translate:

  • Comparisons on mapped scalar properties
  • Casts between supported primitive types
  • Enum-related operations that map cleanly to the database
  • Entity inheritance patterns that are represented in the model

What it generally cannot translate is arbitrary CLR behavior such as:

  • Casting to a custom interface
  • Calling methods that only exist in application code
  • Building complex objects inside the SQL-translated part of the query unless the provider supports the projection pattern

Fix 1: Query on the Actual Mapped Property

If the entity already has the property you need, reference it directly instead of through the interface.

csharp
var query = db.People
    .Where(p => p.Id > 10)
    .Select(p => new { p.Id, p.Name });

This is the simplest and best fix because the query stays fully translatable.

Fix 2: Materialize First, Then Cast

If you truly need the interface at the application layer, move the cast after materialization.

csharp
1var results = db.People
2    .Where(p => p.Id > 10)
3    .AsEnumerable()
4    .Cast<IEntity>()
5    .ToList();

AsEnumerable() switches from LINQ to Entities to LINQ to Objects. After that point, the cast happens in memory, not in SQL translation.

Be careful with this approach. It means the filtering done after AsEnumerable() happens in memory, so you should keep as much filtering as possible on the database side first.

Fix 3: Project to a DTO Instead of an Interface

Often the real goal is not “cast to an interface,” but “return a smaller shape.” In that case, use a DTO.

csharp
1public class PersonDto
2{
3    public int Id { get; set; }
4    public string Name { get; set; } = "";
5}
6
7var results = db.People
8    .Where(p => p.Id > 10)
9    .Select(p => new PersonDto
10    {
11        Id = p.Id,
12        Name = p.Name
13    })
14    .ToList();

This keeps the query translatable and usually makes intent clearer.

Fix 4: Use Proper Entity Inheritance Tools

If the cast is really about mapped inheritance, use EF features such as OfType<TDerived>() rather than a custom interface cast.

csharp
1var admins = db.Users
2    .OfType<AdminUser>()
3    .Where(a => a.IsActive)
4    .ToList();

That works because the inheritance relationship is part of the entity model and can be translated by EF.

Common Pitfalls

A common mistake is assuming that if a cast is legal in C#, it should also be legal in LINQ to Entities. Query providers can only translate a subset of CLR expressions.

Another mistake is calling AsEnumerable() too early. That fixes translation errors, but it can also drag far more data into memory than necessary.

A third mistake is using interfaces as if they were queryable schema concepts. Interfaces are excellent for application design, but they do not automatically become part of the database query model.

Summary

  • The error occurs because Entity Framework cannot translate custom interface casts such as IEntity into SQL.
  • Query directly on mapped entity properties whenever possible.
  • If you need interface behavior, materialize first and cast afterward.
  • DTO projections are often a cleaner alternative than interface casting.
  • Use EF inheritance features such as OfType<T>() when the cast represents model inheritance.

Course illustration
Course illustration

All Rights Reserved.