Is there support for paging with OData in Cosmos DB?

爱⌒轻易说出口 提交于 2020-07-03 05:21:09

问题


I can see there is support for offset/limit when accessing a Cosmos DB in Azure via the SQL API - but does OData support this yet?


回答1:


UPDATE

You can download my demo in github. And this article and offical document can help u.

Data in My Storage account

Test by postman

TestDataController.cs

public class TestDataController : ODataController
{
    [EnableQuery]
    public IHttpActionResult Get()
    {
        CloudStorageAccount account = CloudStorageAccount.Parse("DefaultEndpointsProtocol=***x=core.windows.net");
        CloudTableClient tableClient = account.CreateCloudTableClient();
        //table name
        CloudTable table = tableClient.GetTableReference("test");
        // all datas in table
        IQueryable<CustomerEntity> linqQuery = table.CreateQuery<CustomerEntity>().Where(x => x.PartitionKey != "0")
        .Select(x => new CustomerEntity() { PartitionKey = x.PartitionKey, RowKey = x.RowKey, Name = x.Name, Role = x.Role });
        // test data
        //var result = CreateTestData().AsQueryable();
        // real data in `test` table
        var a = linqQuery.ToList<CustomerEntity>().AsQueryable();
        return Ok(a);
    }

    public List<TestData> CreateTestData()
    {
        List<TestData> data = new List<TestData>();
        data.Add(new TestData { Id = 1, Name = "Jignesh", Role = "Project Manager" });
        data.Add(new TestData { Id = 2, Name = "Tejas", Role = "Architect" });
        data.Add(new TestData { Id = 3, Name = "Rakesh", Role = "Lead" });

        return data;
    }
}

WebApiConfig.cs

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapODataServiceRoute("odata", null, GetEdmModel(), new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
        config.EnsureInitialized();


    }
    private static IEdmModel GetEdmModel()
    {
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.Namespace = "WebAPITest";
        builder.ContainerName = "DefaultContainer";
        builder.EntitySet<TestData>("TestData");
        // you can dunamic load entitys later
        builder.EntitySet<CustomerEntity>("CustomerEntity");
        var edmModel = builder.GetEdmModel();
        return edmModel;
    }
}

PRIVIOUS

I am not clear about this solution. What application will you use, desktop or web application?

If your app is web application, you can see these article.(offical document , Paging With OData And ASP.NET Web API )

If your app not web application. I suggest u use linq to solve the issue.

    public static async Task Main(string[] args)
    {
        Console.WriteLine("Azure Cosmos Table Samples");
        CloudStorageAccount account = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=*****fix=core.windows.net");
        CloudTableClient tableClient = account.CreateCloudTableClient();
        CloudTable table = tableClient.GetTableReference("test");
        IQueryable<CustomerEntity> linqQuery = table.CreateQuery<CustomerEntity>().Where(x => x.PartitionKey != "0")
        .Select(x => new CustomerEntity() { PartitionKey = x.PartitionKey, RowKey = x.RowKey, Name = x.Name });
       // skip and take method 
       var c = linqQuery.ToList<CustomerEntity>().Skip(3).Take(1).ToList<CustomerEntity>();
        Console.Read();
    }


来源:https://stackoverflow.com/questions/62047510/is-there-support-for-paging-with-odata-in-cosmos-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!