oData 的 skip() 和 top() 在过滤之前拉取整个记录集

oData's skip() and top() pulling entire record set before filtering

我有一个启用 oData 的网络 api 功能

[EnableQuery()]
    public IQueryable<StoreCommand> Get()
    {
        return _storeCommandService.GetAllStoreCommands().AsQueryable();
    }

服务层调用 Mongodb 基于存储库模式的实现。

public IEnumerable<StoreCommand> GetAllStoreCommands()
    {
        return _uow.StoreCommands.GetAll();
    }

其中 GetAll 在 Repository 层中实现,如

    public IList<TEntity> GetAll()
    {
        return _collection.FindAllAs<TEntity>().ToList();
    }

其中 _collection 是 c# 驱动程序的 MongoCollection。

当我打电话时

http://localhost:xxxx/api/storeCommandsrest?$skip=0&$top=10&$orderby=Name

我得到了前 10 条记录,但它从数据库中提取了所有记录并返回前 10 条记录。 请指导我们如何只从数据库中提取所需的集合。

评论已移至答案:

您没有return从 GetAllStoreCommands() 获取 IQueryable。您的 return 类型必须是 IQueryable()。要从驱动程序那里得到它,它应该是 _collection.AsQueryable().