In this example we are going to test a query where the data-source is grouped on a single property, that intern create a new-group and finally we write a sub-query to this new-group

In the previous example doc_mm.LINQIntro20B.htm, we noticed that simple join did not group collectively as one to many relation, like one teacher will teach many student, here instead of using GroupJoin we used group--into clause to obtain similar results. Here students were grouped with a common subject reference.

KeyWords

  • Array Class  for a data-source: 
    • Class : public Student[] GetStudent() {
    • Array : Student[] student = { new Student { Name----; here
  • group into
    • group std by std.Subject into stdGroup
  • sub-query on Grouped datasource
  • Max Min

Step: 1 Create a new website and use the codes below to complete the application.

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 + " &nbsp Max Avg :&nbsp" + item.HighScore.ToString() + "&nbsp<br/>&nbsp&nbsp Student: &nbsp";
foreach (var item2 in item.StudentName)
{
L1.Text += item2 + "&nbsp";
}
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 + " &nbsp Min Avg &nbsp" + item.LowScore.ToString() + "&nbsp<br/> &nbsp&nbsp Student: &nbsp";
foreach (var item2 in item.StudentName)
{
L1.Text += item2 + "&nbsp";
}
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" >
&nbsp; Grouping : This is an extension of mm.LINQIntro13, mm.LINQIntro20B,
gruping a single property using &quot;into&quot;, </td>
</tr>
<tr>
<td id="td1" ><asp:Label ID="L1" runat="server" Text=""></asp:Label></td>
<td id="td2">&nbsp;</td>
</tr>

</table>
</div>
</form>
</body>
</html>
 

Step: 3 Run time assessment.

Step: Key Points

Step:

Step:

Step:

Step: