Step: Codes:
Atylesheet: main.cs
body
{
background-color:Gray;
}
#div1
{
background-color: #FFFFCC; color: #000080; position: absolute;
width:600px; height: 450px;
padding-left: 20px; border-color:Maroon; border-style:solid; border-width:
4px; font-size:16px;
}
table
{
width : 600px; vertical-align:top;
}
#td1
{
width :600px;vertical-align:top; text-align:left;
}
#td2
{
width : 100px;vertical-align:top; text-align:left;
}
#GridView1
{
width : 200px; border-style: solid; border-width:2px; border-color:Navy;
}
Class : Student.cs (also an array-method data-source)
using System;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
public class Student
{
public string Name { get; set; }
public string Subject { get; set; }
public int ID { get; set; }
public List<int> Scores { get; set; }
public Student()
{
// TODO: Add constructor logic here
}
public Student[] GetStudent()
{
Student[] student =
{ new Student { Name="Daniel",Subject="Chemistry",ID=8, Scores= new List<int>
{96, 92, 94, 99} },
new Student { Name="David",Subject="Chemistry",ID=8 , Scores= new List<int>
{94, 92, 94, 87} },
new Student { Name="Brandon",Subject="Chemistry",ID=8 , Scores= new List<int>
{99, 92, 94, 89} },
new Student { Name="Zared",Subject="English",ID=4 , Scores= new List<int>
{99, 90, 94, 92} },
new Student { Name="Lipika",Subject="English",ID=4 , Scores= new List<int>
{99, 97, 94, 94} },
new Student { Name="Jane",Subject="English",ID=4 , Scores= new List<int>
{99, 98, 94, 96} },
new Student { Name="Vishal",Subject="History",ID=1 , Scores= new List<int>
{99, 92, 93, 95} } };
return student;
}
}
Code: Default.aspx.cs
using System;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Student students = new Student();
Student[] st = students.GetStudent();
//
var query1 = from std in st
group std by std.Subject into stdGroup
select new
{
StudentName = (from stname in stdGroup select stname.Name),
Level = stdGroup.Key,
HighScore = (from std2 in stdGroup select std2.Scores.Average()).Max()
};
foreach (var item in query1)
{
// key is the subject here
L1.Text += item.Level + "   Max Avg : " + item.HighScore.ToString()
+ " <br/>   Student:  ";
foreach (var item2 in item.StudentName)
{
L1.Text += item2 + " ";
}
L1.Text += "<br/>";
}
//Min
var query2 = from std in st where std.Subject=="Chemistry"
group std by std.Subject into stdGroup
select new
{
StudentName = (from stname in stdGroup select stname.Name),
Level = stdGroup.Key,
LowScore = (from std2 in stdGroup select std2.Scores.Average()).Min()
};
L1.Text += "<br/>---Min()--<br/>";
foreach (var item in query2)
{
// key is the subject here
L1.Text += item.Level + "   Min Avg  " + item.LowScore.ToString()
+ " <br/>    Student:  ";
foreach (var item2 in item.StudentName)
{
L1.Text += item2 + " ";
}
L1.Text+="<br/>";
}
}
}
Default.aspx.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>mm.LINQIntro20C : groupin and group method</title>
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div id="div1">
<table >
<tr>
<td colspan="2" >
Grouping : This is an extension of mm.LINQIntro13, mm.LINQIntro20B,
gruping a single property using "into", </td>
</tr>
<tr>
<td id="td1" ><asp:Label ID="L1" runat="server" Text=""></asp:Label></td>
<td id="td2"> </td>
</tr>
</table>
</div>
</form>
</body>
</html>