CODE BEHIND FILE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class LinqSumExample : System.Web.UI.Page
{
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var data = from s in GetStudentList()
join m in GetStudentMarkList() on s.ID equals m.StduentID
group new { m, s } by new
{
s.StduentName
} into g
select new
{
StudentName = g.Key.StduentName,
TotalNumber = g.Sum(i => i.m.ObtainNumber)
};
gvSum.DataSource = data;
gvSum.DataBind();
}
}
private List<Student> GetStudentList()
{
List<Student> StudentList = new List<Student>
{
new Student{ID=1, StduentName="Ashikur Rahman"},
new Student{ID=2, StduentName="Roman Mozomder"},
new Student{ID=3, StduentName="Iqbal Hossain"},
};
return StudentList;
}
private List<StudentMark> GetStudentMarkList()
{
List<StudentMark> StudentMarkStudentMarks = new List<StudentMark>{
new StudentMark{StduentID=1, Subject="English", ObtainNumber =88},
new StudentMark{StduentID=1, Subject="Math", ObtainNumber =95},
new StudentMark{StduentID=1, Subject="Accounting", ObtainNumber =65},
new StudentMark{StduentID=2, Subject="English" , ObtainNumber =75},
new StudentMark{StduentID=2, Subject="Math" , ObtainNumber =95},
new StudentMark{StduentID=2, Subject="Accounting" , ObtainNumber =55},
new StudentMark{StduentID=3, Subject="English" , ObtainNumber =88},
new StudentMark{StduentID=3, Subject="Math" , ObtainNumber =85},
new StudentMark{StduentID=3, Subject="Accounting" , ObtainNumber =79}
};
return StudentMarkStudentMarks;
}
public class StudentMark
{
public int StduentID { get; set; }
public string StduentName { get; set; }
public string Subject { get; set; }
public decimal ObtainNumber { get; set; }
}
public class Student
{
public int ID { get; set; }
public string StduentName { get; set; }
}
}