Saturday, August 13, 2011

Using LINQ with ASP.NET

Step 1: Creating your first ASP.NET page using LINQ

Create a new page called Step1.aspx. Within the .aspx page add a GridView control like so:



using System;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Query;

public partial class Step1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string[] cities = { "London", "Amsterdam", "San Francisco", "Las Vegas",

"Boston", "Raleigh", "Chicago", "Charlestown",

"Helsinki", "Nice", "Dublin" };

GridView1.DataSource = from city in cities

where city.Length > 4

orderby city

select city.ToUpper();

GridView1.DataBind();

}

}


LISQ Query with Where


using System;

using System.Query;

public partial class Data_Data2 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Northwind db = new Northwind();

GridView1.DataSource = from x in db.Suppliers

where x.Country == "USA"

orderby x.Country

select new {

x.CompanyName,

x.Country,

x.Products

};

GridView1.DataBind();

}

}

No comments:

Post a Comment