Azure表存储:如何创建动态where子句? [英] Azure Table Storage: How can I create a dynamic where clause?

查看:56
本文介绍了Azure表存储:如何创建动态where子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我是第一次在ASP.NET MVC 3应用程序中使用Azure表存储.

Ok, so I am using Azure Table Storage for the first time in a ASP.NET MVC 3 application.

我有一个表实体,该表实体具有一个用户ID作为其RowKey.我有一个用户ID列表,需要获取所有具有其中一个用户ID的实体.

I have a table entity that has a user ID as its RowKey. I have a list of user IDs and need to get all of the entities that have one of the User IDs.

在传统的SQL中,它将是where子句中的简单OR语句,您可以将其动态添加到其中:

In traditional SQL it would be a simple OR statement in the where clause that you can dynamically add to:

select * from blah
where userID = '123' or userID = '456' or userID = '789'

但是我在Azure SDK中找不到等效项.

but I haven't found the equivalent in the Azure SDK.

Azure表存储可以做到这一点吗?

Is this possible with Azure Table Storage?

谢谢, 大卫

推荐答案

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

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.

希望这对某人有帮助.

大卫

这篇关于Azure表存储:如何创建动态where子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆