/*______________________________________________________________ * G E T T I N G T O K N O W L I N Q Step 2 * * C# VERSION * Ron Kessler * 2/5/09 * * * _____________________________________________________________ */ // This time lets limit the number of columns returned so the grid looks more useful. // BE SURE TO SET THIS AS THE STARTUP PAGE IN THE SOLUTION EXPLORER using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Step2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //---first create a new reference to our datacontext object AdventureworksDataContext dc = new AdventureworksDataContext(); //---now create a LINQ query string. Notice the use a var which creates // an anyonymous type. This time I use p as my variable to make it cleaner. // We will select the exact columns we want from the table. var products = from p in dc.Products where p.ProductCategoryID == 18 select new { p.ProductNumber, p.Name, p.Color, p.ListPrice }; //---now link up the grid to display our data GridView1.DataSource = products; GridView1.DataBind(); //make it so! } }