I'm struggling with this function i've been searching for awhile but for some reason it can't get the outcome i desire. I have a stored procedure which returns the following data. Query Results Img
Here is the class i'm using for the list:
[DataContract]
public class EvtTypes
{
[DataMember]
public string EvtType { get; set; }
[DataMember]
public int EvtCnt { get; set; }
}
Here is the code the makes the call:
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "getEvtTypesCnt")]
List<EvtTypes> GetEvtTypesCnt();
public List<EvtTypes> GetEvtTypesCnt()
{
var data = new StHomeDBDataContext();
var list = new List<EvtTypes>();
foreach (var r in data.GetAllEvtTypesCnt_ToList())
{
list.Add(new EvtTypes
{
EvtType = r.evtType,
EvtCnt = Convert.ToInt32(r.evtCnt)
});
}
return list;
}
which when called returns this:
[
{
"EvtCnt": 18,
"EvtType": "alarmSystemStatus"
},
{
"EvtCnt": 13871,
"EvtType": "battery"
},
{
"EvtCnt": 210,
"EvtType": "button"
},
{
"EvtCnt": 23,
"EvtType": "color"
},
{
"EvtCnt": 3777,
"EvtType": "contact"
},
{
"EvtCnt": 88,
"EvtType": "door"
},
{
"EvtCnt": 103440,
"EvtType": "energy"
},
{
"EvtCnt": 304,
"EvtType": "heatingSetpoint"
},
{
"EvtCnt": 24,
"EvtType": "hue"
}
]
I really want to format it to look like this:
{
"evtType": [{
"name": "name here",
"cnt": 2323
}]
}