Step: 2: Code
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" Debug="true" %>
<!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.LINQIntro22 :AsQueryable to convert an IEnumerable </title>
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div id="div1">
<table style="width:100%;">
<tr>
<td colspan="2" >
List< Student > is Serialized with AsQueryable </td>
</tr>
<tr><td id="td1" > <asp:GridView ID="GridView1" runat="server" >
</asp:GridView>
</td>
<td id="td2"><asp:Label ID="L1" runat="server" Text=""></asp:Label></td>
</tr>
<tr>
<td colspan="2" >
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
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();
List<Student> query2 = new List<Student>(st.AsQueryable());
List<Student> query3 = new List<Student>();
String[] QueryString = { "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight" };
IQueryable<string> query1 = QueryString.AsQueryable();
GridView1.DataSource = st;
GridView1.DataBind();
L1.Text += "<u> --Student[] st = students.GetStudent();</u><br/> ";
foreach (var item in st)
{
L1.Text += item.Name + " subject : " + item.Subject + ", scores : ";
for (int i = 0; i < 3; i++)
{
L1.Text += " " + item.Scores[i].ToString();
}
L1.Text += "<br/>";
}
L1.Text += " <u>--IQueryable< string > query1 </u><br/> ";
foreach (var item in query1)
{
L1.Text += " " + item;
}
L1.Text += " <br/><u> IQueryable < Student > query2 </u><br/> ";
foreach (var item in query2)
{
L1.Text += item.Name + " subject : " + item.Subject + ", scores : ";
for (int i = 0; i < 3; i++)
{
L1.Text += " " + item.Scores[i].ToString() ;
}
L1.Text += "<br/>";
}
L1.Text += " <br/>--- IQueryable< Student > query3 <br/> ";
foreach (var item in query3)
{
L1.Text += item.Name + " subject : " + item.Subject + " ,scores : ";
for (int i = 0; i < 3; i++)
{
L1.Text += " " + item.Scores[i].ToString();
}
L1.Text += "<br/>";
}
}
}
Code Class Student.cs
using System;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
/// <summary>
/// Summary description for Player
/// </summary>
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, 92, 94, 95} },
new Student { Name="Vishal",Subject="History",ID=1 , Scores= new List<int>
{99, 92, 94, 95} } };
return student;
}
}
Code main.css
body
{
background-color:Gray;
}
#div1
{
background-color: #FFFFCC; color: #000080; position: absolute;
width:700px; height: 450px;
padding-left: 20px; border-color:Maroon; border-style:solid; border-width:
4px; font-size:16px;
}
table
{
width : 700px; vertical-align:top;
}
#td1
{
width : 200px;vertical-align:top; text-align:left;
}
#td2
{
width : 500px;vertical-align:top; text-align:left;
}
#GridView1
{
width : 200px; border-style: solid; border-width:2px; border-color:Navy;
}