Cosmos DB中是否支持OData分页?

时间:2020-05-27 15:59:08

标签: c# azure odata azure-cosmosdb azure-cosmosdb-sqlapi

我可以看到通过SQL API在Azure中访问Cosmos DB时支持偏移量/限制-但是OData是否支持此功能?

1 个答案:

答案 0 :(得分:1)

更新

您可以在github中下载my demothis articleoffical document可以为您提供帮助。

“我的存储”帐户中的数据

enter image description here

邮递员测试

enter image description here

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 dynamic load entitys later
        builder.EntitySet<CustomerEntity>("CustomerEntity");
        var edmModel = builder.GetEdmModel();
        return edmModel;
    }
}

重要

我不清楚该解决方案。您将使用桌面或Web应用程序中的哪种应用程序?

如果您的应用是Web应用,则可以查看以下文章。(offical documentPaging With OData And ASP.NET Web API

如果您的应用不是网络应用。我建议您使用linq解决该问题。

enter image description here

enter image description here

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