Select example of LINQ to SQL ( Example uses ASP.NET 3.5, AdventureWorks 2005 Database, C# as assumes that you know basic ASP.NET)
1. Create a new project/website in Visual C#.
2. Add a LINQ To SQL class to your project
3. Drop the products table from database into designer surface. As you drag a new class in to surface explorer, strongly typed datacontext class is generated.
4. Navigate back to default.aspx and drag and drop a gridview.
5. Now add following code on Page_Load event.
6. Complie and execute.
protected void Page_Load(object sender, EventArgs e)
{
MydbDataContext db1 = new MydbDataContext();
var query = db1.Products.Where(p => p.ProductID == 4).Select(p => new { p.ProductID, p.ModifiedDate });
GridView1.DataSource = query ;
GridView1.DataBind();
}
LINQ to SQL Query explanation: This LINQ to SQL query would select two column values from Product table where ProductID is 4
Here’s the output would look like
No comments:
Post a Comment