使用 Trim 的 Azure 表存储中的 C# 查询字符串字段

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

我试图通过检查用户名来查看通过 API 提供的用户是否存在于表存储中。代码看起来与此类似:

public async Task<bool> UserExistsAsync(User user)
{
    var username = user.Username.Trim();
    var query = _usersTableClient.QueryAsync<UserEntity>(u => 
         u.Username.Trim().Equals(username, StringComparison.InvariantCultureIgnoreCase));

    await foreach (var page in query.AsPages())
    {
         if (page.Values.Any())
             return true;
    }

    return false;
}

但是,此代码会返回错误,因为查询 Azure 表存储不支持 Trim() 方法:

不支持 Trim 方法。

我需要以某种方式删除空格,因为表中充满了包含空格的值(例如“SomeUser”)。我无权访问该表,也无法要求客户端删除空格。另外,加载内存中的所有条目并逐一检查它们(并最终在加载后修剪它们)也不是解决方案,因为该表有超过 100k 行。我可以使用任何解决方法来完成此任务吗?

编辑:尝试使用

Contains()Substring(),它们也不被允许。

c# .net azure trim azure-table-storage
1个回答
1
投票
在 azure 中创建一个存储帐户并使用代码中的连接字符串来使用连接。

enter image description here

用于修剪的 C# 代码

TableQuery<Customer> tableQuery = new TableQuery<Customer>(); foreach (Customer customerEntity in table.ExecuteQuery(tableQuery)) { CloudStorageAccount storageAccount = CloudStorageAccount.Parse("storageConnectionString"); CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); CloudTable tbl = tableClient.GetTableReference("tblCustomers"); string name = customerEntity.CustomerName.Trim(); TableOperation tblOperation = TableOperation.Retrieve<Customer>("partitionKey", name); TableResult tr = tbl.Execute(tblOperation); if (tr.Result != null) { Customer user = (Customer)tr.Result; Console.WriteLine("User found: {0}", user.RowKey); } else { Console.WriteLine("User not found: {0}", name); } }
例如,下面突出显示的客户名称有空格。

enter image description here

可以使用下面的代码删除这个空格。

foreach (Customer entity in table.ExecuteQuery(tableQuery)) { entity.CustomerName = entity.CustomerName.Replace(" ", ""); table.Execute(TableOperation.Replace(entity)); }

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.