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 c开发者_如何转开发lass
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.
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();
精彩评论