Thursday, 15 May 2014

c# - Transforming into another object using LINQ Grouping -



c# - Transforming into another object using LINQ Grouping -

i have list of exams has happened on particular time, , mark obtained pupil exam contained in object below:

class exam { public datetime? datetime { get; set; } public double? mark { get; set; } public string pupil { get; set; } public int age { get; set; } }

i need transform format display purposes using next structure:

class examviewmodel { public datetime? datetime { get; set; } public double? mark { get; set; } list<studentviewmodel> students { get; set; } } class studentviewmodel { public string pupil { get; set; } public int age { get; set; } } class resultviewmodel { list<examviewmodel> exams { get; set; } }

basically want ot display exam has taken place @ particuar date, , total marks achieved in pupil (s1 = 10, s2 = 20, total = 30 @ 19/06/2014). students should transformed pupil viewmodel can see students have attended exam. how can using linq?

thanks, -mike

note: using .net4

tried:

exams.orderby(x=>x.datetime) .groupby(x => x.datetime, x=> x, (d, students) => new{date= x, students = students.tolist()} );

but cant total of marks..:(

it's cleaner imho utilize groupby subsequent select instead of using overload of groupby includes projection:

var query = exams.groupby(x => x.datetime) .select(g => new { date = g.key, totalmarks = g.sum(s => s.mark) } );

c# linq

No comments:

Post a Comment