Reputation: 91
Method with variable that accept variety of JSON structure to be query over linq...
Currently my result is heading the right direction returning the select result over the linq query but it's hard coded.
I have fews sets of data structure,
example belows:-
var SET_A=[
{
"Axel": "C019",
"Growth": 4.795,
"Status": "Poor"
},
{
"Axel": "C019",
"Growth": 4.083333,
"Status": "Fair"
},
{
"Axel": "C019",
"Growth": 8.031212,
"Status": "V.Poor"
},
{
"Axel": "C019",
"Growth": 10.6275,
"Status": "V.Poor"
},
{
"Axel": "C019",
"Growth": 3.876363,
"Status": "Fair"
},
{
"Axel": "C019",
"Growth": 7.735714,
"Status": "V.Poor"
},
{
"Axel": "C020",
"Growth": 3.196477,
"Status": "Good"
},
{
"Axel": "C020",
"Growth": 3.2,
"Status": "Good"
},
{
"Axel": "C020",
"Growth": 4.51125,
"Status": "Fair"
},
{
"Axel": "C020",
"Growth": 4.125,
"Status": "Fair"
}
];
another sample data.
var SET_B = [
{
"Class": "H",
"Points": 252.17,
"TotalAchieve": 252.17,
"LastRecord": 2012
},
{
"Class": "A",
"Points": 36.44,
"TotalAchieve": 36.44,
"LastRecord": 2012
},
{
"Class": "B",
"Points": 442.07,
"TotalAchieve": 442.07,
"LastRecord": 2012
},
{
"Class": "C",
"Points": 852.32,
"TotalAchieve": 852.32,
"LastRecord": 2012
},
{
"Class": "D",
"Points": 903.96,
"TotalAchieve": 1323.83,
"LastRecord": 2012
},
{
"Class": "E",
"Points": 0,
"TotalAchieve": 0,
"LastRecord": 2011
},
{
"Class": "J",
"Points": 0,
"TotalAchieve": 0,
"LastRecord": 2011
},
{
"Class": "M",
"Points": 0,
"TotalAchieve": 0,
"LastRecord": 2011
},
{
"Class": "T",
"Points": 0,
"TotalAchieve": 0,
"LastRecord": 2011
},
{
"Class": "T",
"Points": 2486.96,
"TotalAchieve": 2906.83,
"LastRecord": 2012
}
]
Possiblity of few more others variant of data in valid JSON structure
my execution ..
ExtractSeries(SET_A, "Growth","Status","Axel");
or
ExtractSeries(SET_B, "Points","Class","LastRecord");
OR
ExtractSeries(SET_B, "TotalAchieve","Class","LastRecord");
I am stuck at this where i am trying
to make use of the group by in the linq.
public List<string> ExtractSeries(string JSONDs, string seedA,string seedB,string groupby)
{
var jss = new JavaScriptSerializer();
var table = jss.Deserialize<dynamic>JSONDs,
dynamic data = System.Web.Helpers.Json.Decode(jss.Serialize(table));
var result = from x in (IEnumerable<dynamic>)data
select new {
*x.Growth*,
*x.Status*
};
}
I am trying to achieve this "kind" of structure
[{
"Name": "C019",
"Data": [
{
"Growth": 4.796,
"Status": "Poor"
},
{
"Growth": 4.083333,
"Status": "Fair"
},
{
"Growth": 8.031212,
"Status": "V.Poor"
},
{
"Growth": 10.6275,
"Status": "V.Poor"
},
{
"Growth": 3.876363,
"Status": "Fair"
},
{
"Growth": 7.735714,
"Status": "V.Poor"
},
{
"Growth": 3.196477,
"Status": "Good"
},
{
"Growth": 3.2,
"Status": "Good"
},
{
"Growth": 4.51125,
"Status": "Fair"
},
{
"Growth": 4.125,
"Status": "Fair"
}
]
},....more data with same structure above i.e "Name":"C020"
]
or
[
{
"Name": "2012",
"Data": [
{
"Class": "H",
"Points": 252.17
},
{
"Class": "A",
"Points": 36.44
},
{
"Class": "B",
"Points": 442.07
},
{
"Class": "C",
"Points": 852.32
},
{
"Class": "D",
"Points": 903.96
}
]
},
{
"Name": "2011",
"Data": [
{
"Class": "E",
"Points": 0
},
{
"Class": "J",
"Points": 0
},
{
"Class": "M",
"Points": 0
},
{
"Class": "T",
"Points": 0
}
]
}
]
Upvotes: 1
Views: 1319
Reputation: 13381
you can use something like this
private static List<SomeClass> ExtractSeries(string JSONDs, string seedA,string seedB,string groupby)
{
var jss = new JavaScriptSerializer();
return (from item in jss.Deserialize<List<Dictionary<string, object>>>(JSONDs)
select new { val = new Dictionary<string, object>(){{ seedA, item[seedA]}, {seedB, item[seedB] }}, groupKey = item[groupby] } into sampleObj
group sampleObj by sampleObj.groupKey into g
select new SomeClass{ Name = g.Key, Data = g.Select(i=>i.val).ToList() })
.ToList();
}
if you serialize this with new JavaScriptSerializer().Serialize(ExtractSeries(SET_B, "Points","Class","LastRecord"))
as result you get string as expected
where SomeClass
like this
public class SomeClass{
public string Name;
public List<Dictionary<string,object>> Data;
}
Upvotes: 2