c# - 如何在Azure表中允许不同的数据格式?

标签 c# azure azure-table-storage

我创建了一个 azure 的表和队列,并且我有代码可以获取队列中的消息并将其插入表中。该代码有效,但如何解释不同的数据条目?例如。 ID:姓名:年龄:疫苗接种是主要输入,但用户输入姓名:年龄:ID:疫苗接种。

 [FunctionName("Function1")]
        public void Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem, ILogger log)
        {

            try
            {
                bool isVac = false;
                log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");

                // Gets the ID from the myQueueItem 
                string ID = myQueueItem.Substring(0, myQueueItem.IndexOf(getString));
                log.LogInformation($"ID: {ID}");

                myQueueItem = getNewString(myQueueItem);
                log.LogInformation($"New String: {myQueueItem}");

                string name = myQueueItem.Substring(0, myQueueItem.IndexOf(getString));
                log.LogInformation($"Name: {name}");

                myQueueItem = getNewString(myQueueItem);
                log.LogInformation($"New String: {myQueueItem}");

                string age = myQueueItem;
                log.LogInformation($"Age: {age}");

                // Gets the vaccination status from the myQueueItem
                string vac = myQueueItem.Substring(0, myQueueItem.IndexOf(getString));
                log.LogInformation($"Vac: {vac}");
                if (vac.ToUpper() == "TRUE")
                {
                    isVac = true;
                }

                AddToTable(ID, name, int.Parse(age), isVac);
            }
            catch (System.Exception ex)
            {

                log.LogInformation(ex.Message);
            }
        }
        private void AddToTable(string ID, string name, int age, bool isVac)
        {
            string connectionString ="";
            TableClient tableClient = new TableClient(connectionString, "Vac");
            tableClient.CreateIfNotExists();

            var entity = new Person()
            {
                PartitionKey = "South Africa",
                RowKey = ID,
                Name = name,
                Age = age,
                Vacinated = isVac
            };
            tableClient.AddEntity(entity);
        }

我尝试了一种方法来从字符串中获取每个值并重新格式化它,但我没有运气(我不再有代码)。

最佳答案

您需要做的是传递一个 json 作为消息内容。然后,一旦反序列化它,只要属性的名称保持不变,顺序就无关紧要了。

public class Person
{
    public string PartitionKey => "South Africa";

    [JsonProperty("rowKey")]
    public string RowKey { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("age")]
    public string Age { get; set; }

    [JsonProperty("vacinated")]
    public bool Vacinated { get; set; }
}

[FunctionName("Function1")]
public void Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem, ILogger log)
{

    try
    {
        var model = JsonConvert.DeserializeObject<Person>(myQueueItem);

        AddToTable(model);
    }
    catch (System.Exception ex)
    {

        log.LogInformation(ex.Message);
    }
}

private void AddToTable(Person model)
{
    string connectionString ="";
    TableClient tableClient = new TableClient(connectionString, "Vac");
    tableClient.CreateIfNotExists();
    
    tableClient.AddEntity(model);
}

关于c# - 如何在Azure表中允许不同的数据格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74548123/

相关文章:

azure - 为 ServiceBusTransientErrorDetectionStrategy 实现指数重试策略

c# - 如何使用 Azure Iot 获取设备的当前位置

azure - TransactionScope 和 Azure 表存储

Azure 表存储 - 我使用了多少数据?

javascript - 如何使用 Angular js 从 2 个表(在 SQL Server 中)提取数据

c# - 检索哈希值后使用 c# HashAlgorithm

javascript - 主题标签的正则表达式一开始在 C# 中不起作用,但在 Javascript 中起作用

c# - System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

azure - 如何使用 kubectl 获取 Azure 凭据?

c# - 如何将 azure 排队消息发送到 azure sql 数据库