Hi,
Please refer below code.
C#
protected void Page_Load(object sender, EventArgs e)
{
int yearP = 2014;
string jsonData = Jqufunc(2014);
}
public static string Jqufunc(int yearP)
{
try
{
List<Catg_type> categories = new List<Catg_type> {
new Catg_type{ Catg_id = 1,Catg_type1="ComputerScience"},
new Catg_type{ Catg_id = 2,Catg_type1 = "Management"},
new Catg_type{Catg_id = 3, Catg_type1="Finance"}
};
List<Program_type> programTypes = new List<Program_type>
{
new Program_type{ Prog_id=1,Prog_name="Bachelors"},
new Program_type{ Prog_id=2,Prog_name="Masters"}
};
List<Year_info> years = new List<Year_info> {
new Year_info{ year_id = 1,year = 2014},
new Year_info{ year_id = 1,year = 2015},
};
List<Std_info> students = new List<Std_info> {
new Std_info{sid = 1,Catg_id = 1,year_id=1,Prog_id=1},
new Std_info{sid = 2,Catg_id = 2,year_id=1,Prog_id=1},
new Std_info{sid = 3,Catg_id = 3,year_id=1,Prog_id=2},
new Std_info{sid = 4,Catg_id = 3,year_id=1,Prog_id=2},
new Std_info{sid = 5,Catg_id = 3,year_id=1,Prog_id=2},
new Std_info{sid = 6,Catg_id = 2,year_id=1,Prog_id=2},
new Std_info{sid = 7,Catg_id = 2,year_id=1,Prog_id=2},
};
string res = "[";
var b = categories.OrderBy(x => x.Catg_type1);
foreach (var c in b)
{
res += "'" + c.Catg_type1 + "',";
}
res = res.Substring(0, res.Length - 1);
res += "]";
var allprogs = programTypes;
string res2 = "[";
foreach (var pr in allprogs)
{
res2 += "{name: '" + pr.Prog_name + "',";
res2 += "'" + yearP + "',";
var allcats = categories;
res2 += "data:";
var result = (from stdtable in students
join catg in categories on stdtable.Catg_id equals catg.Catg_id
join prog in programTypes on stdtable.Prog_id equals prog.Prog_id
join yea in years on stdtable.year_id equals yea.year_id
where yea.year == yearP && prog.Prog_name == pr.Prog_name
orderby prog.Prog_name
group stdtable by new { catg.Catg_type1, prog.Prog_name, yea.year } into g
select new
{
g.Key.Catg_type1,
g.Key.Prog_name,
g.Key.year,
total_students = g.Count(),
});
string d = string.Empty;
foreach (var item in b)
{
var re = (from rest in result
where rest.Catg_type1 == item.Catg_type1
select rest).FirstOrDefault();
if (re != null)
{
d += re.total_students;
}
else
{
d += "0";
}
d += ",";
}
res2 += d.Remove(d.Length - 1);
res2 += "},";
}
res2 = res2.Remove(res2.Length - 1);
res2 += "]";
return res + res2;
//return null;
}
catch (Exception exception)
{
throw new Exception();
}
}
public class Std_info
{
public int sid { get; set; }
public int Catg_id { get; set; }
public int Prog_id { get; set; }
public int year_id { get; set; }
}
public class Catg_type
{
public int Catg_id { get; set; }
public string Catg_type1 { get; set; }
}
public class Program_type
{
public int Prog_id { get; set; }
public string Prog_name { get; set; }
}
public class Year_info
{
public int year_id { get; set; }
public int year { get; set; }
}
I hope this will help you out.