【问题标题】:Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: System.DateTime timeStamp, double finalState, double consuption>>'无法隐式转换类型'System.Collections.Generic.List<<匿名类型:System.DateTime timeStamp,double finalState,double consuption>>'
【发布时间】: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>
}

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    Total_PowerList 变量类型被声明为 List,因此您不能在之后分配不同的类型。相反,只需创建一个新变量并在局部视图中传递它。

    var KwhList = 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();
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-06
      • 1970-01-01
      • 2023-01-18
      • 2014-01-07
      • 1970-01-01
      • 2019-08-18
      • 2020-05-03
      • 1970-01-01
      相关资源
      最近更新 更多