我正在从InfluxDB数据库中读取数据。该查询的结果是对象类型,我需要将此数据传递给具有两个变量的模块。
型号:
public class DashboardViewModel
{
public string time { get; set; }
public string valor { get; set; }
}
我在其中执行查询的异步任务
public async Task<List<Serie>> ConsultaBD()
{
var _client = new InfluxDb("http://host:8086/", "***", "***");
List<Serie> series = await _client.QueryAsync("dbName", "SELECT T_PV FROM TFA WHERE time >= '2019-05-21' and time < '2019-05-22'");
return series;
}
IActionResult,我将数据传递给模型:
public IActionResult Index()
{
DashboardViewModel DatosParaMostrar = new DashboardViewModel();
DatosParaMostrar.time = ConsultaBD().Result[0].Values[0][0].ToString();
DatosParaMostrar.valor = ConsultaBD().Result[0].Values[0][1].ToString();
return View(DatosParaMostrar);
}
返回的数据“ QueryBD()。Result [0] .Values”是这样的对象:
data->
[0]->
[0]= {21/05/2019 0:00:03}
[1]= 586.5
[1]->
[0]= {21/05/2019 0:00:03}
[1]= 575.4
[2]->
[0]= {21/05/2019 0:00:03}
[1]= 595.5
[3]->
[0]= {21/05/2019 0:00:03}
[1]= 584.25
[4]->
[0]= {21/05/2019 0:00:03}
[1]= 583.625
.......
使用此代码,我仅传递第一个数据,如何传递对象具有的所有数据?
答案 0 :(得分:1)
您应该await
致电ConsultaBD
:
public async Task<IActionResult> Index()
{
DashboardViewModel DatosParaMostrar = new DashboardViewModel();
var result = (await ConsultaBD()).First();
DatosParaMostrar.time = result.Values[0][0].ToString();
DatosParaMostrar.valor = result.Values[0][1].ToString();
return View(DatosParaMostrar);
}
如果要显示所有检索到的数据,不仅要显示第一个数据,还应该填充DashboardViewModel
的列表并将其传递给视图:
public async Task<IActionResult> Index()
{
var results = await ConsultaBD();
IList<DashboardViewModel> model = results.Select(x => new DashboardViewModel {
time = x.Values[0][0].ToString(),
valor = x.Values[0][1].ToString()
}).ToList();
return View(model);
}
答案 1 :(得分:0)
型号:
-E
IActionResult:
$: sed -Ei 's/,\s+/,/' file.csv
$: cat file.csv
[2,4,5,6,2,34,61,32,34,54,34,22] 0.144354
[3,4,6,4,5,6,7,1,2,3,4,53,23,34] 0.332453
[2,43,6,2,1,2,5,8,9,0,8,6,34,21] 0.347643
在您的public class DashboardViewModel
{
public string time { get; set; }
public string valor { get; set; }
public List<DashboardViewModel> lstDashboardviewModel {get; set; }
}
中迭代public IActionResult Index()
{
DashboardViewModel DatosParaMostrar = new DashboardViewModel();
foreach(var data in ConsultaBD().Result){
DatosParaMostrar.time = data.Values[0].ToString();
DatosParaMostrar.valor = data.Values[1].ToString();
DatosParaMostrar.lstDatosParaMostrar.Add(DatosParaMostrar);
}
return View(DatosParaMostrar);
}
中的列表并进行相应的绑定。