且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Azure表存储:如何创建动态where子句?

更新时间:2023-02-15 10:04:21

好吧,我进行了进一步的挖掘,找到了答案.

Alrighty, with a bit more digging I found the answer.

您可以使用以下语法构造一个where过滤器: http://msdn.microsoft.com/zh-CN/library/windowsazure/ff683669.aspx

You can construct a where filter using the syntax found here: http://msdn.microsoft.com/en-us/library/windowsazure/ff683669.aspx

所以对于我的小例子,它最终看起来像这样:

So for my little example it ended up looking like this:

我有一个用逗号分隔的ID字符串,已发送给此方法

I have a comma delimited string of IDs sent to this method

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["TableStorageConnectionString"]);

CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("Blah");

string[] split = IDs.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

string filter = null;
for (int i = 0; i < split.Length; i++)
{
    filter += " RowKey eq '" + split[i] + "' ";
    if (i < split.Length - 1)
        filter += " or ";
}

TableQuery<Blah> rangeQuery = new TableQuery<Blah>().Where(filter);
var result = table.ExecuteQuery(rangeQuery);

结果列出了我需要的东西.

Result has the list of goodies I need.

要记住的一件事是,您不想在非常大的表上使用它,因为我只是得到了引起表扫描的RowKey.如果同时使用PartitionKey和RowKey,则效率更高.我的表很小(最多几百个记录),所以这不应该成为问题.

One thing to keep in mind is that you wouldn't want to use this on a really large table because I am only getting the RowKey which causes a table scan. If you use the PartitionKey and RowKey together it is more efficient. My table is pretty small (few hundred records at most) so it shouldn't be an issue.

希望这对某人有帮助.

大卫