我创建了一个 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/