0

I have two tables that are joined by a link table and exposed through OData / Entity Framework:

  • USERS
  • USERGROUP

Using ~/api/Users, the following [USER] API controller action returns results:

    public IEnumerable<USER> Get(ODataQueryOptions<USER> options)
    {
        var unitOfWork = new ATMS.Repository.UnitOfWork(_dbContext);

        var users = options.ApplyTo(unitOfWork.Repository<USER>().Queryable
                                              .Include(u => u.USERGROUPS)
                                              .OrderBy(order => order.USERNAME))
                                              .Cast<USER>().ToList();

        unitOfWork.Save();      // includes Dispose()

        return users;
    }

I am, however, unable to apply the ODataQueryOptions to the following [USERGROUP] API controller action:

    public IEnumerable<USERGROUP> Get(ODataQueryOptions<USER> options)
    {
        var unitOfWork = new ATMS.Repository.UnitOfWork(_dbContext);

        var userGroups = options.ApplyTo(unitOfWork.Repository<USERGROUP>().Queryable
                                .Include(u => u.USERS)
                                .OrderBy(order => order.GROUPNAME))
                                .Cast<USERGROUP>().ToList();

        unitOfWork.Save();      // includes Dispose()

        return userGroups.AsQueryable();
    }

When I do, I get the following error:

Cannot apply ODataQueryOptions of 'DAL.USER' to IQueryable of 'DAL.USERGROUP'.

Instead, I have to omit the options.ApplyTo(...):

        var userGroups = unitOfWork.Repository<USERGROUP>()
                                .Query()
                                .Include(u => u.USERS)
                                .Get()
                                .OrderBy(order => order.GROUPNAME);

Can someone please explain to me why this is?

Thanks.

4

1 回答 1

1

错误消息说明了一切。您不能在一组用户组上应用旨在应用于一组用户的查询。将参数类型更改为ODataQueryOptions<USERGROUP>

于 2013-08-19T19:14:54.983 回答