将类型 'int?' 隐式转换为 'int'
Implicitly convert type 'int?' to 'int'
为此,我尝试以 json 格式获取数据 我尝试使用此网络方法代码
格式:
[{name: 'May',data: [23]}, {name: 'June', data: [43]}, {name: 'July', data: [45]}]
代码:
try
{
var data = new Datas1().spsumdata().Select(s => new { name = s.Month, data = new int[] {s.data} }).ToArray();
return Newtonsoft.Json.JsonConvert.SerializeObject(data);
}
catch(Exception)
{
throw new Exception();
}
但这显示错误:
Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)
当我在模型中添加存储过程然后创建函数导入时我select复杂类型
[EdmComplexTypeAttribute(NamespaceName="TrackDataModel", Name="spsumdata_Result")]
[DataContractAttribute(IsReference=true)]
[Serializable()]
public partial class spsumdata_Result : ComplexObject
{
#region Simple Properties
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public global::System.String OwnerName
{
get
{
return _OwnerName;
}
set
{
OnOwnerNameChanging(value);
ReportPropertyChanging("OwnerName");
_OwnerName = StructuralObject.SetValidValue(value, true, "OwnerName");
ReportPropertyChanged("OwnerName");
OnOwnerNameChanged();
}
}
private global::System.String _OwnerName;
partial void OnOwnerNameChanging(global::System.String value);
partial void OnOwnerNameChanged();
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public global::System.String Month
{
get
{
return _Month;
}
set
{
OnMonthChanging(value);
ReportPropertyChanging("Month");
_Month = StructuralObject.SetValidValue(value, true, "Month");
ReportPropertyChanged("Month");
OnMonthChanged();
}
}
private global::System.String _Month;
partial void OnMonthChanging(global::System.String value);
partial void OnMonthChanged();
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public Nullable<global::System.Int32> data
{
get
{
return _data;
}
set
{
OndataChanging(value);
ReportPropertyChanging("data");
_data = StructuralObject.SetValidValue(value, "data");
ReportPropertyChanged("data");
OndataChanged();
}
}
private Nullable<global::System.Int32> _data;
partial void OndataChanging(Nullable<global::System.Int32> value);
partial void OndataChanged();
#endregion
}
data
属性 是 int?
类型,但您正试图将其添加到 int
.
类型的数组中
您需要使 int?
类型的数组匹配或将 data
值转换为 int
。
var data = new Datas1().spsumdata()
.Select(s => new
{
name = s.Month,
// take your pick
//data = new int?[] { s.data },
//data = new int[] { s.data ?? 0 },
//data = new int[] { s.data.Value },
}).ToArray();
Cannot implicitly convert type 'int?' to 'int'.
'int?'
表示支持integer
和null
值,如果要将其转换为整数。
A = B??0;
其中 A
为 int
,B
为 int?
B??0
表示当B
包含null
值时,它转换为0
值。
为此,我尝试以 json 格式获取数据 我尝试使用此网络方法代码
格式:
[{name: 'May',data: [23]}, {name: 'June', data: [43]}, {name: 'July', data: [45]}]
代码:
try
{
var data = new Datas1().spsumdata().Select(s => new { name = s.Month, data = new int[] {s.data} }).ToArray();
return Newtonsoft.Json.JsonConvert.SerializeObject(data);
}
catch(Exception)
{
throw new Exception();
}
但这显示错误:
Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)
当我在模型中添加存储过程然后创建函数导入时我select复杂类型
[EdmComplexTypeAttribute(NamespaceName="TrackDataModel", Name="spsumdata_Result")]
[DataContractAttribute(IsReference=true)]
[Serializable()]
public partial class spsumdata_Result : ComplexObject
{
#region Simple Properties
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public global::System.String OwnerName
{
get
{
return _OwnerName;
}
set
{
OnOwnerNameChanging(value);
ReportPropertyChanging("OwnerName");
_OwnerName = StructuralObject.SetValidValue(value, true, "OwnerName");
ReportPropertyChanged("OwnerName");
OnOwnerNameChanged();
}
}
private global::System.String _OwnerName;
partial void OnOwnerNameChanging(global::System.String value);
partial void OnOwnerNameChanged();
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public global::System.String Month
{
get
{
return _Month;
}
set
{
OnMonthChanging(value);
ReportPropertyChanging("Month");
_Month = StructuralObject.SetValidValue(value, true, "Month");
ReportPropertyChanged("Month");
OnMonthChanged();
}
}
private global::System.String _Month;
partial void OnMonthChanging(global::System.String value);
partial void OnMonthChanged();
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public Nullable<global::System.Int32> data
{
get
{
return _data;
}
set
{
OndataChanging(value);
ReportPropertyChanging("data");
_data = StructuralObject.SetValidValue(value, "data");
ReportPropertyChanged("data");
OndataChanged();
}
}
private Nullable<global::System.Int32> _data;
partial void OndataChanging(Nullable<global::System.Int32> value);
partial void OndataChanged();
#endregion
}
data
属性 是 int?
类型,但您正试图将其添加到 int
.
您需要使 int?
类型的数组匹配或将 data
值转换为 int
。
var data = new Datas1().spsumdata()
.Select(s => new
{
name = s.Month,
// take your pick
//data = new int?[] { s.data },
//data = new int[] { s.data ?? 0 },
//data = new int[] { s.data.Value },
}).ToArray();
Cannot implicitly convert type 'int?' to 'int'.
'int?'
表示支持integer
和null
值,如果要将其转换为整数。
A = B??0;
其中 A
为 int
,B
为 int?
B??0
表示当B
包含null
值时,它转换为0
值。