如何在C#中使用OData过滤器查询表

问题描述 投票:0回答:1

如何在C#(doc)中使用OData过滤器查询表?请提供相同的示例吗?

c# azure-table-storage
1个回答
0
投票

您可以使用LinQ像所支持的文档一样进行查询。

    static async Task Main(string[] args)
    {
        Console.WriteLine("Azure Cosmos Table Samples");
        Console.WriteLine("Query data by filter");

        CloudTable table = GetTable();

        Console.WriteLine("pls input PartitionKey:");
        string PartitionKey = Console.ReadLine();
        Console.WriteLine("pls input RowKey:");
        string RowKey = Console.ReadLine();
        //Query
        IQueryable<CustomerEntity> linqQuery = table.CreateQuery<CustomerEntity>()
        .Where(x => x.PartitionKey== PartitionKey && x.RowKey== RowKey)
        .Select(x => new CustomerEntity() { PartitionKey = x.PartitionKey, RowKey = x.RowKey, Email = x.Email, PhoneNumber= x.PhoneNumber });

        var list = linqQuery.ToList<CustomerEntity>();

        foreach (CustomerEntity m in list)
        {
            Console.WriteLine(m.PartitionKey+"    "+m.PhoneNumber+"    "+m.RowKey+"   "+m.Email);
        }
        Console.WriteLine();
        Console.WriteLine("Press any key to exit");
        Console.Read();
    }

    public static CloudTable GetTable() {
        CloudStorageAccount account = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=***x=core.windows.net");
        CloudTableClient tableClient = account.CreateCloudTableClient();
        CloudTable table = tableClient.GetTableReference("People");
        return table;
    }
© www.soinside.com 2019 - 2024. All rights reserved.