DolceVita
DolceVita

Reputation: 2100

DataServiceContext & Linq: Select only inherited objects

I use WCF DataSerice and EF4.1 code-first (C#).

I have 2 entities: 1. Person 2. User inherited from Person class

I want to get only User objects from DbSet Persons. How can I do that? Then I query dataervice like that:

service.CreateQuery<User>("DbPersons").OfType<User>().Execute();

I get an exception that OfType method is not supported by dataservice context.

Error translating Linq expression to URI: The method 'OfType' is not supported.

Upvotes: 0

Views: 1491

Answers (1)

DolceVita
DolceVita

Reputation: 2100

Ok, solved by adding service operation method. See solution here http://msdn.microsoft.com/en-us/library/cc668788.aspx

here is my code:

      public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetServiceOperationAccessRule("DbUsers", ServiceOperationRights.AllRead);
    }

    [WebGet]
    public IQueryable<User> DbUsers()
    {
        return CurrentDataSource.DbPersons.OfType<User>();
    }

and in your client application send request as usual:

service.CreateQuery<User>("DbUsers").Execute();

Upvotes: 2

Related Questions