When students are dynamic, this means that the result class needs a class of results:
class ResultRow
{
public ResultRow()
{
Students = new Dictionary<string, string>();
}
public string Class { get; set; }
public IDictionary<string, string> Students { get; set; }
}
Because you need dynamic columns for students.
Now I can generate a result similar to your expected result with this code:
var res = tblClass
.GroupBy(g=> g.ClassId)
.Select(c =>
{
var rr = new ResultRow {Class = c.Key};
foreach (var r in tblClass.GroupBy(gg=> gg.Student))
{
rr.Students[r.Key] = "N";
}
foreach (var r in c.Where(w=> w.ClassId == c.Key))
{
rr.Students[r.Student] = "Y";
}
return rr;
})
.toList();
You can also add these two methods to the class ResultRow:
public string GetHeader()
{
return Students.Aggregate("Class", (current, s) => current + "|" + s.Key);
}
public string GetSolidRow()
{
return Students.Aggregate(Class, (current, s) => current + "|" + s.Value);
}
[ Demo Here]
NTN
source
share