开发者

DataServiceContext & Linq: Select only inherited objects

开发者 https://www.devze.com 2023-03-26 07:49 出处:网络
I use WCF DataSerice and EF4.1 code-first (C#). I have 2 entities: 1. Person 2. User inherited from Person c开发者_如何转开发lass

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号