This example is an extension of doc_mm.LINQIntro8.htm. Here we used a for loop to serialize a list object  " List<Student> " of a class called "Student" . We also used "AsQueryable" operator, that allows the List object to be Enumerable with for loops. Also note that

KeyWords:

  • List<Student> query2 = new List<Student>(st.AsQueryable());
  • IQueryable<string> query1 = QueryString.AsQueryable();
  • The following code example demonstrates how to use AsQueryable<(Of <(TElement>)>)(IEnumerable<(Of <(TElement>)>)) to convert an IEnumerable<(Of <(T>)>) to an IQueryable<(Of <(T>)>).

Step: 1 Create a new web site, add a class and style sheet, as shown in the image below. Then add following components in the Default.aspx page.

  • Table
  • One Label and GridView control in the table.

 

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" >
&nbsp; List< Student > is Serialized with AsQueryable </td>
</tr>
<tr><td id="td1" >&nbsp<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" >
&nbsp;</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;
}

Step: 3 Runt time analysis

Step: 4 Also note that grid does not process the List, implicitly.

It does not matter whether we used AsQueryable or simple array, results remained same.

Step: 5

Step: 6

Step: 7