转换为值类型 'System.Single' 失败,因为存储过程的具体化值为 null

The cast to value type 'System.Single' failed because the materialized value is null for stored procedure

我的存储过程returns以下数据:

SensorNodeUUID         Type  val0   val1    val2
--------------------------------------------------
88418344615647248       3   25.77   0       4.23
88418344615634456       3   NULL    NULL    NULL
88432552356623423       2   NULL    NULL    NULL
88418344584627440       3   24.77   0       4.29

我有一个模型:

  public class EditSensorModel 
    {
        public List<EditSensorModel> editsensor;

        public Int64 SensorNodeUUID { get; set; }
        public int Type { get; set; }
        public float val0 { get; set; }
        public float val1 { get; set; }
        public float val2 { get; set; }
}

在控制器中:

List<EditSensorModel> vendlist = new List<EditSensorModel>();
        var vnlist = entities.Database.SqlQuery<EditSensorModel>("exec usp_getsensornode @userid",
            new SqlParameter("@userid", Convert.ToInt32(Session["Userid"]))
     ).ToList();

                    foreach (var item in vnlist)
                    {
                    EditSensorModel temp = new EditSensorModel();
                    temp.SensorNodeUUID = item.SensorNodeUUID;
                    temp.Type = item.Type;
                    temp.val0 = item.val0 ;
                    temp.val1 = item.val1 ;
                    temp.val2 = item.val2 ;
                    vendlist.Add(temp);
                    }

在控制器中,我在将数据存储到变量 vnlist 中时遇到错误。 我面临的错误是:转换为值类型 'System.Single' 失败,因为物化值为 null。 有谁知道如何解决这个错误

使用浮动?在你的模型中。

float? val0;
float? val1;
float? val2;