views:

64

answers:

2

In my generic abstract class

class SomeClass<T> where T : ISomeInterface

I have a method that calls my repository and pass a parameter Expression<Func<T, bool>> parameter to tell him the 'where' condition.

As T implements ISomeInterface, the DebugView of the lambda passed brings me something like

...((ISomeInterface)$p).SomeInterfaceMember == ...

This causes Entity Framework throw an exception
Unable to cast the type 'MySomeTypeThatImplementsISomeInterfaceAndIsPassedAs[T]ToTheClass' to type 'ISomeInterface'. LINQ to Entities only supports casting Entity Data Model primitive types.

The method that I call is IRepository<T>.Get(t => t.SomeInterfaceMember == 1)

Thanks for helping

A: 

I'm not sure exactly what's causing your problem but I use an interface which is shown below and have no issues...

public interface IRepository<T>
{
    IQueryable<T> Get(System.Linq.Expressions.Expression<System.Func<T, bool>> Query);
}

I can then just call my XRepository as follows:

XRepository.Get(x => x.member = 1);

So I don't think it's the concept that's at fault unless I've misunderstood your question?

NB make sure you call the Get() method on your concrete repository class not the interface itself. You said: "The method that I call is IRepository<T>.Get(t => t.SomeInterfaceMember == 1)"

Basiclife
Why is your example interface in VB for a C# question? :)
Kirk Woll
Because I was about to leave work and I had a VB interface literally in front of me - And it's trivially simple to convert one way or the other in your head but copy/paste allowed me to leave rapidly. If it'll make you happy, I'll edit it :p
Basiclife
A: 

Just like the error message says, LINQ to Entities does not recognize your custom interface type. It only recognizes entity types and certain scalar types, like string and DateTime. You cannot pass an Expression which uses other types to a queryable object which will be executed by LINQ to Entities. So your Expression needs to use other types.

Craig Stuntz