Question

I have a project where the client is using Entity Framework, and I'm trying to abstract away the generated classes from the rest of the application.

One generated class is Category and it has say Type as a property.

I've created an interface that I want Category to implement, like this:

public interface ICategory
{
    string Type { get; set;}
}

I have done this in LINQ to SQL before and it works fine. I create a partial class in a separate file and have it implement the interface:

public partial class Category: ICategory
    //implement interface

However, with EF whenever I try to build a query with EF it says it doesn't support OfType<>().

Example:

var query = from c in DataContext.Category
            where Type == "some type"
            select c;

var resultsList = query.OfType<ICategory>(); //error here (not supported)

What am I doing wrong here?

Other things to note: I'm developing this in a silverlight application and the data context is actually being pulled from a service, so there's a client server relationship going on here as well.

Was it helpful?

Solution

As a general rule, LINQ to Entities can only understand things which are part of your entity model (EDMX). So while you are free to extend your entity types be a partial classes, you cannot use properties, methods, and interface references you add there in LINQ to Entities queries, except for certain, very specific features.

However, in this case the following query should give you the result you want:

var resultsList = query.Select<ICategory>(c => c);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top