我撰寫了一個使用外部 API 的程式,效果很好。我瀏覽了 SO 和其他資源,但無法解決我的問題。我對 c# 很陌生。我創建了GetEquipment存盤從 API 回傳的資料的模型,EquipmentDb這是一個我想要插入資料的 SQL 表。我試圖以這種方式保存它:
消費 API:
public async Task<IEnumerable<GetEquipment>> GetDataFromAPI()
{
try
{
var token = await _accountService.GetToken(
_configuration["CredentialsXY:client_id"],
_configuration["CredentialsXY:client_secret"]);
var equipment = await _callAPIService.GetData(token);
return equipment;
}
catch (Exception e)
{
Debug.Write(e);
return Enumerable.Empty<GetEquipment>();
}
}
模型 - SQL 表:
public partial class EquipmentDb
{
public string SerialNo { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
}
模型 - 從 API 回傳的資料:
public class GetEquipment
{
public Item[] items { get; set; }
}
public class Item
{
public string serial_number { get; set; }
public Location location { get; set; }
}
public class Location
{
public float longitude { get; set; }
public float latitude { get; set; }
}
將資料保存到資料庫:
public async Task Index()
{
var result = await GetDataFromAPI(); // result from API
using (var ctx = new HUDAContext())
{
var equipment = new EquipmentDb(); // SQL table
foreach(var item in result)
{
equipment.SerialNo = item.items.serial_number, // here I iterate through 'result' where I store data from API but it does not work, 'items' is highlighted
equipment.Latitude = item.items.location.latitude,
equipment.Latitude = item.items.location.longitude
};
ctx.EquipmentDbs.Add(equipment);
await ctx.SaveChangesAsync();
}
}
插入資料不起作用,我嘗試將資料分配給result我的 SQL 表中的每個欄位,但它不起作用。
uj5u.com熱心網友回復:
從問題中不清楚資料是如何形成的。要理解的重要一點是你有兩個集合包裝器:IEnumerable在 API 結果中,其中包括Item[]它的內部。你必須看看這兩者的內部。
但是,我懷疑您只期望一個結果或另一個結果(同樣:從問題中不清楚)。考慮到這一點,你可能想要這個:
public async Task Index()
{
var result = await GetEquipment(); // result from API
var transformed = result.Select(e =>
new EquipmentDB {
SerialNo = e.items[0].serial_number, // here I iterate through 'result' where I store data from API but it does not work, 'items' is highlighted
Latitude = e.items[0].location.latitude,
Latitude = e.items[0].location.longitude
}
);
using var ctx = new HUDAContext();
foreach(var item in transformed)
{
ctx.EquipmentDbs.Add(item);
await ctx.SaveChangesAsync();
}
}
或這個:
public async Task Index()
{
var result = await GetEquipment(); // result from API
var transformed = result.First().Select(e =>
new EquipmentDB {
SerialNo = e.serial_number, // here I iterate through 'result' where I store data from API but it does not work, 'items' is highlighted
Latitude = e.location.latitude,
Latitude = e.location.longitude
}
);
using var ctx = new HUDAContext();
foreach(var item in transformed)
{
ctx.EquipmentDbs.Add(item);
await ctx.SaveChangesAsync();
}
}
再一次,你可能真的需要同時做這兩個(看看SelectMany()這個),并訪問可列舉中的每個元素,并且對于這些元素中的每一個,訪問陣列中的每個專案。
uj5u.com熱心網友回復:
假設您使用的是 EF,并且 EquipmentDB 是您想要在 foreach 中創建一個新的 equipmentDB 物件的背景關系類,例如:
foreach(var item in result)
{
ctx.EquipmentDbs.Add(new EquipmentDb
{
SerialNo = item.items.serial_number,
Latitude = item.items.location.latitude,
Latitude = item.items.location.longitude
});
};
否則你只是覆寫同一個物件,只會保存最后一個結果。
然后在使用新填充串列完成回圈后呼叫保存:)!
- 還假設您的意思是 GetDataFromAPI 而不是 GetEquipment()
uj5u.com熱心網友回復:
嘗試先將其保存到串列中,然后在背景關系中呼叫 AddRange
public async Task Index(){
var result = await GetEquipment(); // result from API
using (HUDAContext ctx = new HUDAContext())
{
List<EquipmentDb> equipmentlist = new List<EquipmentDb>()
foreach(var item in result)
{
equipmentlist.add(new EquipmentDb{
SerialNo = item.items.serial_number,
Latitude = item.items.location.latitude,
Longitude = item.items.location.longitude
})
};
ctx.EquipmentDbs.AddRange(equipmentlist);
await ctx.SaveChangesAsync();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/438714.html
上一篇:如何在C#中使用Dictionary進行自定義訂單?
下一篇:如何將引數從視圖提供給控制器?
