I am trying to create a JSON feed to return to Google charts a multi line chart, with four series. I am struggling to form the correct JSON to return to the charts.
I am using a stored procedure that produces a long list of the data I require to show in the chart. I want to be able to take the long list of data and convert it into four separate data series and send back to the chart in JSON.
My Model:
public class FoodCountCompare
{
[JsonProperty("Count")]
public int Count { get; set; }
[JsonProperty("Day")]
public string Day { get; set; }
[JsonProperty("Type")]
public string Type { get; set; }
}
My SPROC:
SELECT COUNT(P) AS 'Count', DATENAME(DW, fldTimeStamp) AS 'Day', #P AS 'Type' FROM [dbname].[FoodTypes] WHERE Protein = 1 AND fldUserId = ''+ #UserId+'' GROUP BY DATENAME(DW, fldTimeStamp)
UNION ALL
SELECT COUNT(H) AS 'Count', DATENAME(DW, fldTimeStamp) AS 'Day', #H AS 'Type' FROM [dbname].[FoodTypes]WHERE HEC = 1 AND fldUserId = ''+ #UserId+'' GROUP BY DATENAME(DW, fldTimeStamp)
UNION ALL
SELECT COUNT(L) AS 'Count', DATENAME(DW, fldTimeStamp) AS 'Day', #L AS 'Type' FROM [dbname].[FoodTypes]WHERE LEC = 1 AND fldUserId = ''+ #UserId+'' GROUP BY DATENAME(DW, fldTimeStamp)
UNION ALL
SELECT COUNT(O) AS 'Count', DATENAME(DW, fldTimeStamp) AS 'Day', #F AS 'Type' FROM [dbname].[FoodTypes]WHERE OmegaFA = 1 AND fldUserId = ''+ #UserId+'' GROUP BY DATENAME(DW, fldTimeStamp)
In my C# code I am then creating four separate lists, which I am sure is wrong. Each list contains the correct data, so this part works. I have tried playing around with arrays and serializations, but I am stuck on how to take this further.
[Function(Name = "[dbname].[FoodCountCompare]")]
public List<FoodCountCompare> GetFoodCountCompare(){
string UserId = User.Identity.GetUserId();
var dc = new DataClasses1DataContext();
var x = dc.FoodCountCompare(UserId).ToList();
var Protein = (from a in x where a.Type == "P" select new { a.Count, a.Day, a.Type }).ToList();
var HEC = (from a in x where a.Type == "H" select new { a.Count, a.Day, a.Type }).ToList();
var LEC = (from a in x where a.Type == "L" select new { a.Count, a.Day, a.Type }).ToList();
var F = (from a in x where a.Type == "O" select new { a.Count, a.Day, a.Type }).ToList();
//Add code to create JSON here
JavaScriptSerializer js = new JavaScriptSerializer();
string p = js.Serialize(Protein);
string h = js.Serialize(HEC);
string l = js.Serialize(LEC);
string f = js.Serialize(F);
return something_like_json;
}
public JsonResult FoodCountCompare(){
var items = new List<FoodCountCompare>(GetFoodCountCompare());
return Json(items, JsonRequestBehavior.AllowGet);
}
For me the question is, how do you take this data and turn it into JSON, such a a nested array or straight set of data.
You mean something like:
[Function(Name = "[dbname].[FoodCountCompare]")]
public List<FoodCountCompare> GetFoodCountCompare(){
string UserId = User.Identity.GetUserId();
var dc = new DataClasses1DataContext();
var x = dc.FoodCountCompare(UserId).ToList();
var items = x.Select(d => new FoodCountCompare { Type = d.Type, Day = d.Day, Count = d.Count })
return items;
}
public JsonResult FoodCountCompare(){
var items = new List<FoodCountCompare>(GetFoodCountCompare());
return Json(new {
Protein = items.Where(d => d.Type == "P"),
Hec = items.Where(d => d.Type == "H"),
Lec = items.Where(d => d.Type == "L"),
F = items.Where(d => d.Type == "O"),
}, JsonRequestBehavior.AllowGet);
}
Which will result in 4 different arrays for each type:
{"Protein":[ ... ],"Hec":[ ... ], "Lec":[ ... ], "F":[ ... ]}
I believe you don't need to specify the exact json property names.
I am making a group by linq statement where i convert a single list of data into an list with a nested list. Here is my code so far:
[TestMethod]
public void LinqTestNestedSelect2()
{
// initialization
List<combi> listToLinq = new List<combi>() {
new combi{ id = 1, desc = "a", name = "A", count = 1 },
new combi{ id = 1, desc = "b", name = "A", count = 2 },
new combi{ id = 2, desc = "c", name = "B", count = 3 },
new combi{id = 2, desc = "d", name = "B", count = 4 },
};
// linq group by
var result = (from row in listToLinq
group new { des = row.desc, count = row.count } by new { name = row.name, id = row.id } into obj
select new A { name = obj.Key.name, id = obj.Key.id, descriptions = (from r in obj select new B() { des = r.des, count = r.count }).ToList() }).ToList();
// validation of the results
Assert.AreEqual(2, result.Count);
Assert.AreEqual(2, result[0].descriptions.Count);
Assert.AreEqual(2, result[0].descriptions.Count);
Assert.AreEqual(2, result[1].descriptions.Count);
Assert.AreEqual(2, result[1].descriptions.Count);
}
public class A
{
public int id;
public string name;
public List<B> descriptions;
}
public class B
{
public int count;
public string des;
}
public class combi
{
public int id;
public string name;
public int count;
public string desc;
}
This is fine if the objects are small like the example. However I will implement this for objects with a lot more properties. How can I efficiently write this statement so I don't have to write field names twice in my linq statement?
I would like to return the objects in the statement and I want something like:
// not working wishfull thinking code
var result = (from row in listToLinq
group new { des = row.desc, count = row.count } by new { name = row.name, id = row.id } into obj
select new (A){ this = obj.key , descriptions = obj.ToList<B>()}).ToList();
Background: I am re writing a web api that retrieves objects with nested objects in a single database call for the sake of db performance. It's basically a big query with a join that retrieves a crap load of data which I need to sort out into objects.
probably important: the ID is unique.
EDIT:
based on the answers so far I have made a solution which sort of works for me, but is still a bit ugly, and I would want it to be better looking.
{
// start part
return (from row in reader.AsEnumerable()
group row by row.id into grouping
select CreateA(grouping)).ToList();
}
private static A CreateA(IGrouping<object, listToLinq> grouping)
{
A retVal = StaticCreateAFunction(grouping.First());
retVal.descriptions = grouping.Select(item => StaticCreateBFunction(item)).ToList();
return ret;
}
I hope the StaticCreateAFunction is obvious enough for what it does. In this scenario I only have to write out each property once, which is what I really wanted. But I hope there is a more clever or linq-ish way to write this.
var result = (from row in listToLinq
group new B { des = row.desc, count = row.count } by new A { name = row.name, id = row.id } into obj
select new A { name = obj.Key.name, id = obj.Key.id, descriptions = obj.ToList() }).ToList();
You can add to each of the A and B classes a constructor that receives a combi and then it takes from it only what it needs. For example for a:
public class A
{
public A(combi c)
{
id = c.id;
name = c.name;
}
}
public class B
{
public B(combi c)
{
count = c.count;
des = c.desc;
}
}
Then your query can look like:
var result = (from row in listToLinq
group row by new { row.id, row.name } into grouping
select new A(grouping.First())
{
descriptions = grouping.Select(item => new B(item)).ToList()
}).ToList();
If you don't like the grouping.First() you can then override Equals and GetHashCode and then in the group by do by a new a with the relevant fields (which will be those in the Equals) and then add a copy constructor from a
Another way, in which you decouple the A/B classes from the combi is to extract the convert logic to a collection of static methods.
hallo i have a linq query like this :
var data = from d in db.DailyBonus
where d.Date>= startDate1 && d.Date<=endDate1
group d by new { Em = d.Employee.Id } into g
select new BasisViewModel
{
EmployeeId = g.Key.Em,
EmployeeNumber = g.FirstOrDefault().Employee.EmployeeNumber,
FullName = g.FirstOrDefault().Employee.FullName,
HK1 = (decimal)g.FirstOrDefault().ManDays,
Basis1 = (decimal)g.FirstOrDefault().BonusBase,
HK2 = (
(from d in db.DailyBonus
where d.Date>= startDate2 && d.Date<=endDate2
group d by new { Em = d.Employee.Id } into g2
select new
{
Mandays = g.FirstOrDefault().ManDays <-- this is decimal type
}
),
};
the problem is , i just want to select one record for HK2 and assigned to Mandays in subquery but there is an error bellow :
Cannot implicitly convert type
'System.Linq.IQueryable' to 'decimal'
what is the solution ? thank you
Instead of returning an anonymous class (new {...})
select new
{
Mandays = g.FirstOrDefault().ManDays <-- this is decimal type
}
return a Decimal you want:
select g.FirstOrDefault().ManDays;
You should add another FirstOrDefault() in the end and use simple value instead of anonymous type:
var data = from d in db.DailyBonus
where d.Date>= startDate1 && d.Date<=endDate1
group d by new { Em = d.Employee.Id } into g
select new BasisViewModel
{
EmployeeId = g.Key.Em,
EmployeeNumber = g.FirstOrDefault().Employee.EmployeeNumber,
FullName = g.FirstOrDefault().Employee.FullName,
HK1 = (decimal)g.FirstOrDefault().ManDays,
Basis1 = (decimal)g.FirstOrDefault().BonusBase,
HK2 = (
(from d in db.DailyBonus
where d.Date>= startDate2 && d.Date<=endDate2
group d by new { Em = d.Employee.Id } into g2
select g.FirstOrDefault().ManDays <-- this is simple value now
).FirstOrDefault(),<--this is another first or default
};
Is there anyway to group the xml node value by similar into number? I had tried to output the value as below but i can't get the out as what i want. Please guide me on this. Thanks!
My Answer_Data Sample in database: (After extract will get the C1 and C2)
Question_ID | Answer_Data
==============================================
1 | <Answer_Data><Answer>C1</Answer>
2 | <Answer_Data><Answer>C2</Answer>
3 | <Answer_Data><Answer>C2</Answer>
String[] data after extract using Linq:
["c1","c2","c2"]
Able to View in MVC:
c1
c2
c2
What i want is:
c1 - 1
c2 - 2
My Controller:
public ActionResult SURV_Answer_Result(int Survey_ID, string Language = "ENG")
{
List<AnswerQuestionViewModel> viewmodel = new List<AnswerQuestionViewModel>();
var query = from r in db.SURV_Question_Ext_Model
join s in db.SURV_Question_Model
on r.Qext_Question_ID equals
s.Question_ID
select new { r, s };
var viewModel = new AnswerQuestionViewModel();
viewModel.Survey_ID = Survey_ID;
string[] resultanswer = new string[queryResult.Count()];
foreach (var item in query.ToList())
{
string str = item.s.Answer_Data;
XElement qconfig;
qconfig = XElement.Parse(str);
string value = item.s.Question_Type;
int i = 0;
switch (value)
{
case "Choices":
{
XElement ChoicesType =
(from node in qconfig.Elements("ChoicesType")
select node).SingleOrDefault();
viewModel.ChoiceType = ChoicesType.Value;
XElement ChoicesAns =
Here is i get the answer data ===>> (from node in qconfig.Elements("Answer")
select node).SingleOrDefault();
resultanswer[i++] = ChoicesAns.Value;
viewModel.ResultAnswer = resultanswer;
}
break;
case "Multiple_Line":
{
// do nothing
}
break;
viewmodel.Add(new AnswerQuestionViewModel()
{
ResultAnswer = viewModel.ResultAnswer
});
}
return View(viewmodel);
}
}
My View:
if (Model[i].ChoiceType == "SingleChoice")
{
for (int x = 0; x < Model[i].ResultAnswer.Count(); x++)
{
#Html.LabelFor(m => m[i].Answer, Model[i].ResultAnswer[x].ToString(),new{ #class="qlabel2" })
<br/>
}
}
As you are stating in question that you are receiving array of element than you just try group by like this
string[] strarray = new string[] {"c1","c2","c2"};
var groups = from str in strarray
group str by str into g
select new {
key = g.Key,
count = g.Count()
}
try group by using linq like this
var xmlstr="<root><Answer_Data><Answer>C1</Answer></Answer_Data>
<Answer_Data><Answer>C2</Answer></Answer_Data>
<Answer_Data><Answer>C2</Answer></Answer_Data></root>";
XDocument xmldoc = XDocument.Parse(xmlstr);
var groups = from record in xmldoc.Descendants("Answer_Data")
group record by (string)record.Element("Answer")
into g
select new {
key = g.Key,
count = g.Count()
}
I have searched through many topics, find some relevant answers too, but I am still not able to reach to a solution, hence I am posting this question.
Problem Description
EmployeeResponse1 = [{"Ques":"1","Rating":"7"},{"Ques":"2","Rating":"1"},{"Ques":"3","Rating":"6"},{"Ques":"4","Rating":"1"},{"Ques":"5","Rating":"1"},{"Ques":"6","Rating":"1"},{"Ques":"7","Rating":"7"},{"Ques":"8","Rating":"1"},{"Ques":"9","Rating":"1"},{"Ques":"10","Rating":"1"},{"Ques":"11","Rating":"1"},{"Ques":"12","Rating":"1"},{"Ques":"13","Rating":"1"},{"Ques":"14","Rating":"1"},{"Ques":"15","Rating":"1"},{"Ques":"16","Rating":"10"}]
EmployeeResponse2 = [{"Ques":"1","Rating":"5"},{"Ques":"2","Rating":"4"},{"Ques":"3","Rating":"7"},{"Ques":"4","Rating":"8"},{"Ques":"5","Rating":"5"},{"Ques":"6","Rating":"9"},{"Ques":"7","Rating":"10"},{"Ques":"8","Rating":"4"},{"Ques":"9","Rating":"9"},{"Ques":"10","Rating":"6"},{"Ques":"11","Rating":"6"},{"Ques":"12","Rating":"6"},{"Ques":"13","Rating":"7"},{"Ques":"14","Rating":"7"},{"Ques":"15","Rating":"9"},{"Ques":"16","Rating":"8"}]
I have these two JSON strings in c# (there can be more).. Now I want to make a final JSON string which will be like:
EmployeeResponseAvg = [{"Ques":"1","Rating":"6"},{"Ques":"2","Rating":"2.5"},{"Ques":"3","Rating":"6.5"},{"Ques":"4","Rating":"4.5"},{"Ques":"5","Rating":"3"},{"Ques":"6","Rating":"5"},{"Ques":"7","Rating":"8.5"},{"Ques":"8","Rating":"2.5"},....,{"Ques":"16", "Rating": "9"}]
like I want rating of Ques = 1 should be the average of Rating (Ques = 1 of string 1) and Rating (Ques = 1 of string 2)... similarly for other questions
i.e like FINAL =[{ QUES = 1, RATING = (Emp1(Rating.WHERE(QUES = 1), Emp2(Rating.WHERE(QUES = 1),).AVERAGE),....................}]
Work So Far
MODEL -> SurveyResponse.cs
public class SurveyResponse
{
public string Ques { get; set; }
public string Rating { get; set; }
}
public class ResponseDataCalls
{
public static SurveyResponse PutData(string t, string v)
{
SurveyResponse s = new SurveyResponse();
s.Ques = t;
s.Rating = v;
return s;
}
}
WebAPI RevGroupChartController.cs
public class RevGroupChartController : ApiController
{
private hr_toolEntities _db = new hr_toolEntities();
public object Get(int cid, int gid)
{
spiderChart obj = new spiderChart();
var group_employees = (from ge in _db.hrt_group_employee
where ge.fk_group_id == gid
select ge.fk_employee_id).ToList();
List<string> EMP = new List<string>();
List<string> SUP = new List<string>();
List<SurveyResponse> EmpResponse = new List<SurveyResponse>();
List<SurveyResponse> SupResponse = new List<SurveyResponse>();
List<List<SurveyResponse>> tmpEMP = new List<List<SurveyResponse>>();
List<List<SurveyResponse>> tmpSUP = new List<List<SurveyResponse>>();
foreach(var emp in group_employees)
{
int eid = Convert.ToInt32(emp);
var Data = (from d in _db.hrt_cycle_response
join g in _db.hrt_cycle_groups on d.hrt_cycle.pk_cycle_id equals g.fk_cycle_id
where d.fk_cycle_id == cid && g.fk_group_id == gid && d.fk_employee_id == eid
select new
{
d.response_employee_answers,
d.response_supervisor_answers
}).First();
EMP.Add(Data.response_employee_answers);
SUP.Add(Data.response_supervisor_answers);
}
foreach(var e in EMP)
{
//tmpEMP = new JavaScriptSerializer().Deserialize<TEMP>(e);
var s = new JavaScriptSerializer();
List<SurveyResponse> em = s.Deserialize<List<SurveyResponse>>(e);
tmpEMP.Add(em);
}
foreach (var s in SUP)
{
//tmpSUP = new JavaScriptSerializer().Deserialize<TEMP>(s);
var e = new JavaScriptSerializer();
List<SurveyResponse> sp = e.Deserialize<List<SurveyResponse>>(s);
tmpSUP.Add(sp);
}
var empl = _db.hrt_questions.Select(x => new { x.question_name }).ToList();
List<int[]> Emprating = new List<int[]>();
//int avgRating;
int cnt = 0;
foreach(var item in tmpSUP)
{
int noofQ = item.Count;
int[] i = new int[noofQ];
for (int y = 0; y > tmpSUP.Count; y++)
{
i[y] = Convert.ToInt32(item[cnt].Rating);
}
Emprating.Add(i);
cnt++;
}
//obj.Employee = Data.response_employee_answers;
//obj.Supervisor = Data.response_supervisor_answers;
obj.ques = new List<object>();
for (int i = 0; i < empl.Count; i++)
{
obj.ques.Add(empl[i].question_name);
}
return obj;
}
public class TEMP
{
public List<SurveyResponse> data { get; set; }
}
}
Explanation of Code
I pass a cycle ID and a group ID...
Each group has more than 1 employee and each employee has a supervisor
so if say group ID 1023 has 2 employees.
Now we have 2 employees and 2 supervisors
we have a json record for each of them
LIKE DB TABLE RESPONSE {fk_emp_id, fk_sup_id, cycle_id, emp_reponse(json), supervisor_response(json)}
so I need to make ONE JSON string for employees (which contains the average of all ratings)
and ONE JSON string for SUPERVISOR (again, average of both the JSONs)
there could be any number of employees, depending on the group size
and each employee will always have a supervisor
In short I want a a string like:
FinalEmployeeResponse = [{'Ques': '1', 'Rating': 'R1'}, {'Ques': '2', 'Rating': 'R2'}, {'Ques': '3', 'Rating': 'R3'}, {'Ques': '4', 'Rating': 'R4'}, ........, {'Ques': '16', 'Rating': 'R16'}]
Here, R1 = AVERAGE(Emp1json.Rating.WHERE('Ques' = 1), Emp2json.Rating.WHERE('Ques' = 1), .....)
and
R2 = AVERAGE(Emp1json.Rating.WHERE('Ques' = 2), Emp2json.Rating.WHERE('Ques' = 2), .....)
... and so on....
Looking forward to your responses.
I am new on stack overflow, please ask for more details if I have missed something.
The correct way to do this is to parse this as JSON. The quick and dirty way is:
static void Main(string[] args)
{
string json1 = #"[{""Ques"":""1"",""Rating"":""7""},{""Ques"":""2"",""Rating"":""1""},{""Ques"":""3"",""Rating"":""6""},{""Ques"":""4"",""Rating"":""1""},{""Ques"":""5"",""Rating"":""1""},{""Ques"":""6"",""Rating"":""1""},{""Ques"":""7"",""Rating"":""7""},{""Ques"":""8"",""Rating"":""1""},{""Ques"":""9"",""Rating"":""1""},{""Ques"":""10"",""Rating"":""1""},{""Ques"":""11"",""Rating"":""1""},{""Ques"":""12"",""Rating"":""1""},{""Ques"":""13"",""Rating"":""1""},{""Ques"":""14"",""Rating"":""1""},{""Ques"":""15"",""Rating"":""1""},{""Ques"":""16"",""Rating"":""10""}]";
string json2 = #"[{""Ques"":""1"",""Rating"":""5""},{""Ques"":""2"",""Rating"":""4""},{""Ques"":""3"",""Rating"":""7""},{""Ques"":""4"",""Rating"":""8""},{""Ques"":""5"",""Rating"":""5""},{""Ques"":""6"",""Rating"":""9""},{""Ques"":""7"",""Rating"":""10""},{""Ques"":""8"",""Rating"":""4""},{""Ques"":""9"",""Rating"":""9""},{""Ques"":""10"",""Rating"":""6""},{""Ques"":""11"",""Rating"":""6""},{""Ques"":""12"",""Rating"":""6""},{""Ques"":""13"",""Rating"":""7""},{""Ques"":""14"",""Rating"":""7""},{""Ques"":""15"",""Rating"":""9""},{""Ques"":""16"",""Rating"":""8""}]";
string averages = AverageNodes(json1, json2);
Console.WriteLine(averages);
Console.ReadKey();
}
private static string AverageNodes(params string[] json)
{
var regex = new Regex(#"(""Ques"":""(?<question>\d+)"",""Rating"":""(?<rating>\d+)"")", RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
var ANUs = regex.Matches(string.Join("", json))
.Cast<Match>()
.Select(m => new { Question = m.Groups["question"].Value, Rating = int.Parse(m.Groups["rating"].Value) })
.GroupBy(a => a.Question, a => a.Rating)
.Select(a => string.Format("{{\"Ques\":\"{0}\",\"Rating\":\"{1}\"}}", a.Key, a.Average()));
return "[" + string.Join(",", ANUs) + "]";
}
I found a 1 line answer to this using LINQ.
double _avg1 = tmpEMP.Select(x => Convert.ToInt32(x.ElementAt(i).Rating)).Average();