如何在 C# 中迭代数组值并更新多条记录
how to iterate array values and update multiple records in C#
我想根据数组值更新多条记录。 “userid”和“orderid”是用逗号分隔的数组值。我想遍历数组值并更新 firestore 的记录。 userid 和 order id 将具有相同的索引。用户数量越多,orderid 的数量就越多。
示例 webapi
https://localhost:44308/AssignTruck/djK2BjrTTnuLSqD8LbeZ,djK2BjrTTnuLSqD8LbeZ/PdTXq6Rk0WfJK9FQV8vt,TmHhOapTNaZco4PcCF8j/S-1
userid - djK2BjrTTnuLSqD8LbeZ,djK2BjrTTnuLSqD8LbeZ
docid - PdTXq6Rk0WfJK9FQV8vt,TmHhOapTNaZco4PcCF8j
代码
[HttpGet("/AssignTruck/{userid}/{orderid}/{shipid}")]
public async Task<ActionResult> AssignTruck(string userid, string orderid, string shipid)
{
{
FirestoreDb db = FirestoreDb.Create("trucks");
DocumentReference docRef = db.Collection("users/" + userid + "/orders").Document(orderid);
DocumentSnapshot snapshot = await docRef.GetSnapshotAsync();
Truck truck = null;
if (snapshot.Exists)
{
truck = snapshot.ConvertTo<Truck>();
truck.Docid = snapshot.Id;
truck.shipId = shipid;
}
else
{
}
await docRef.SetAsync(truck);
return RedirectToAction("tables");
}
}
假设userids和orderids对应同一个索引,你的代码应该是这样的:
[HttpGet("/AssignTruck/{userid}/{orderid}/{shipid}")]
public async Task<ActionResult> AssignTruck(string userid, string orderid, string shipid)
{
string[] uIds= userid.Split(',');
string[] oIds= orderid.Split(',');
for(int i=0; i<uIds.Length(); i++) {
FirestoreDb db = FirestoreDb.Create("trucks");
DocumentReference docRef = db.Collection("users/" + uIds[i] + "/orders").Document(oIds[i]);
DocumentSnapshot snapshot = await docRef.GetSnapshotAsync();
Truck truck = null;
if (snapshot.Exists)
{
truck = snapshot.ConvertTo<Truck>();
truck.Docid = snapshot.Id;
truck.shipId = shipid;
}
await docRef.SetAsync(truck);
}
return RedirectToAction("tables");
}
我想根据数组值更新多条记录。 “userid”和“orderid”是用逗号分隔的数组值。我想遍历数组值并更新 firestore 的记录。 userid 和 order id 将具有相同的索引。用户数量越多,orderid 的数量就越多。
示例 webapi
https://localhost:44308/AssignTruck/djK2BjrTTnuLSqD8LbeZ,djK2BjrTTnuLSqD8LbeZ/PdTXq6Rk0WfJK9FQV8vt,TmHhOapTNaZco4PcCF8j/S-1
userid - djK2BjrTTnuLSqD8LbeZ,djK2BjrTTnuLSqD8LbeZ
docid - PdTXq6Rk0WfJK9FQV8vt,TmHhOapTNaZco4PcCF8j
代码
[HttpGet("/AssignTruck/{userid}/{orderid}/{shipid}")]
public async Task<ActionResult> AssignTruck(string userid, string orderid, string shipid)
{
{
FirestoreDb db = FirestoreDb.Create("trucks");
DocumentReference docRef = db.Collection("users/" + userid + "/orders").Document(orderid);
DocumentSnapshot snapshot = await docRef.GetSnapshotAsync();
Truck truck = null;
if (snapshot.Exists)
{
truck = snapshot.ConvertTo<Truck>();
truck.Docid = snapshot.Id;
truck.shipId = shipid;
}
else
{
}
await docRef.SetAsync(truck);
return RedirectToAction("tables");
}
}
假设userids和orderids对应同一个索引,你的代码应该是这样的:
[HttpGet("/AssignTruck/{userid}/{orderid}/{shipid}")]
public async Task<ActionResult> AssignTruck(string userid, string orderid, string shipid)
{
string[] uIds= userid.Split(',');
string[] oIds= orderid.Split(',');
for(int i=0; i<uIds.Length(); i++) {
FirestoreDb db = FirestoreDb.Create("trucks");
DocumentReference docRef = db.Collection("users/" + uIds[i] + "/orders").Document(oIds[i]);
DocumentSnapshot snapshot = await docRef.GetSnapshotAsync();
Truck truck = null;
if (snapshot.Exists)
{
truck = snapshot.ConvertTo<Truck>();
truck.Docid = snapshot.Id;
truck.shipId = shipid;
}
await docRef.SetAsync(truck);
}
return RedirectToAction("tables");
}