Take the 2-minute tour ×

I am building my query using PredicateBuilder from LinqKit. it is great and does exactly what i am looking for.

To make my code more reusable (tables and views) i created a generic predicate builder class:

public class LocalPredicateBuilder<T> where T : IResort
    var predicate = PredicateBuilder.True<T>(

which exposes BuildPredicate method. I can use it like this:

var predicate = new LocalPredicateBuilder<Resort>().BuildPredicate();
var resorts = _entities.Resorts.Where(predicate).ToList();

however when i try to do this, i get this runtime error (btw entity objects implement IResort): Unable to cast the type 'ConsoleApplication1.Entities.Resort' to type 'ConsoleApplication1.Entities.IResort'. LINQ to Entities only supports casting Entity Data Model primitive types

i tried casting (didn't work):

var rlist = eq.Cast<Resort>().ToList();

Any other way i can get around this casting issue?


not having much luck getting predicates to work using interfaces.. so i solved my problem by going with POCOs.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Well, the error is accurate. You can't do that in an L2E query, because your interface (IReport) is not part of your entity model and hence can't be converted to SQL. You have to use an entity type, not an interface for that.

share|improve this answer
add comment

just create a partial class for the entity frameowrk object and make that implement the interface.

the other way would be to create a list of the type you want

then do a for each on the linq dataset and add the items to the collection.

the problem is caused because .net doesnt know how to cast List<ISomething> into a List<Something>

share|improve this answer
i have. wouldn't be able to compile otherwise –  Sonic Soul Oct 22 '10 at 14:06
ok then its the problem i have added after my edit. –  John Nicholas Oct 22 '10 at 14:30
the query actually returns List<Something> so it should be possible to make that cast. the ISomething is only used to create predicates used in where query. –  Sonic Soul Oct 22 '10 at 16:45
add comment

Your Answer


