LINQ queries with where clause

main.cs

Class

Code LinqQuery.cs

using System;
using System.Data;


/// <summary>
/// Summary description for LinqQuery
/// </summary>
public class LinqQuery
{
public int sid { get; set; }
public string Job { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Note { get; set; }

public LinqQuery()
{
//
// TODO: Add constructor logic here
//
}

public LinqQuery(int sid, string Job, string firstname, string lastname, string note)
{
//
// TODO: Add constructor logic here
//
this.sid = sid; this.Job = Job;
this.FirstName = firstname;
this.LastName = lastname; this.Note = note;
}

}
 

main.css

 form
{
width:500px; background-color:#FFFFCC; height:auto;

}
#div1
{
position:absolute; background-color: #FFFFCC;z-index:2;
top: 20px; left: 10px;width: 500px; height:auto;
padding-left:10px; padding-bottom:10px;
}

body
{
background-color: Gray;

}

table
{
width:450px;
}

#GridView1
{
border-color: #CC6600; border-style:solid;
width:450px; border-width:6px;


}
#GridView2
{
border-color: #CC6600; border-style:solid;
width:450px; border-width:6px; top:250px;


}
 

Code: 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 id="Head1" runat="server">
<title>mm.LINQ6 : List and LINQ Query</title>
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div id="div1"> Reaular Query : <br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView> <br /> Creating Query with LINQ <br />
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>

Code: default.aspx.cs

using System;
using System.Data;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var varLinq = GetAll();
this.GridView1.DataSource = varLinq;
this.GridView1.DataBind();
// linq Queries
var qLinq = GetAll();
var lquery = from m in qLinq where m.sid > 1002 select m;
this.GridView2.DataSource = lquery;
this.GridView2.DataBind();
}
public List<LinqQuery> GetAll()
{
//List<adoList> results = new List<adoList>();
LinqQuery adoObj = new LinqQuery();

//adoObj.strResult += "Data Loaded";
return new List<LinqQuery>
{new LinqQuery(adoObj.sid = 1001, adoObj.Job = "Manager", adoObj.FirstName = "John", adoObj.LastName = "Smith", adoObj.Note = "10 years Experience"),
new LinqQuery(adoObj.sid= 1002, adoObj.Job = "Stock Analyst", adoObj.FirstName = "Linda", adoObj.LastName = "Thomas", adoObj.Note = "Finance"),
new LinqQuery(adoObj.sid = 1004, adoObj.Job = "President", adoObj.FirstName = "Peter", adoObj.LastName = "Jones", adoObj.Note = "Higher Education"),
new LinqQuery(adoObj.sid = 1003, adoObj.Job = "Video Tech", adoObj.FirstName = "Emanul", adoObj.LastName = "Frisco", adoObj.Note = "Video Grapher")
};


}

}
 

Run time conformataion