user6628729 user6628729 - 4 months ago 23
JSON Question

Implicitly convert type 'int?' to 'int'

I try to get data in json format for this i try this web method code

format:

[{name: 'May',data: [23]}, {name: 'June', data: [43]}, {name: 'July', data: [45]}]


code:

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();
}


but this shows an error as below


Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)


When I add store procedure in model and then create function import i select complex type

[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

}

Answer

The data property is of type int? but you're attempting to add it to an array of type int.

You need to either make the array of type int? to match or convert the data value to 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();