views:

30

answers:

1

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.

+1  A: 

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);
Craig Stuntz
I'm not trying to change the queries that I'm issuing, I'm just trying to change the result. I do all my querying first, and at the end when i'm finished I change the result to my interface, but not before I've gotten the results I want.
Joseph
Yes, I understand. How does what I suggest not work for that?
Craig Stuntz
I still get the NotSupported Exception, but I think I figured out the problem, but it has to do with Service References, and not actually the entity framework.
Joseph