【发布时间】:2020-12-05 12:03:40
【问题描述】:
我想将列表数据传递给视图模型,但在控制器方法中将其转换为列表类时出现错误。 错误 -
错误 CS0029 无法隐式转换类型 'System.Collections.Generic.List>' to 'System.Collections.Generic.List
'
c#
public ActionResult KWHDateWise(RangeSelection RS)
{
DateTime ToDate = Convert.ToDateTime(RS.ToDate).AddDays(1);
RS.ToDate = ToDate;
List<Total_Power> Total_PowerList = db.Total_Power
.Where(u => u.DeviceImei == RS.DeviceImei &&
(u.DeviceTimeStamp >= RS.FromDate && u.DeviceTimeStamp <= RS.ToDate))
.OrderByDescending(u => u.DeviceTimeStamp)
.ToList();
ViewBag.Total_PowerList = Total_PowerList
.GroupBy(
i => i.DeviceTimeStamp.Date,
(timeStamp, dayilyData) => new { timeStamp, dayilyData })
.Select(i => new
{
i.timeStamp,
finalState = i.dayilyData.Max(x => x.KWH),
consuption = (i.dayilyData.Max(x => x.KWH) - i.dayilyData.Min(x => x.KWH))
})
.ToList();//getting error in this line
return PartialView("~/Views/TransEnergyHistory/_KWH.cshtml", Total_PowerList);
}
型号
[Table("Total_Power")]
public class Total_Power
{
[Key]
public int A_id { get; set; }
public string DeviceImei { get; set; }
[DisplayFormat(DataFormatString = "{0:dd MMM yyyy HH:mm tt}")]
public DateTime DeviceTimeStamp { get; set; }
public double KWH { get; set; }
public double KVARH { get; set; }
public double Sum_I { get; set; }
public double KW { get; set; }
public double KVA { get; set; }
public double KVAR { get; set; }
public double MPD { get; set; }
public double MKVAD { get; set; }
}
查看
@model IEnumerable<TransformerEnergyMonitoring.Models.Total_Power>
@foreach (var item in @ViewBag.Total_PowerList)
{
<tbody>
@foreach (var item in ViewBag.Total_PowerList)
{
<tr>
<td>
@item
</td>
</tr>
}
</tbody>
}
【问题讨论】: