In this blog you will find ASP.NET 3.5 articles and some code samples. These code samples are created by me and should be working. Thanks for visiting my blog! Happy Dot Netting

Wednesday, January 14, 2009

LINQ to SQL - Performing Select Queries

This blog is a extension of my last blog* and would describe how you can make select queries with LINQ to SQL. I have added four queries here which would give to insight about how to select a particular row or Column or if there is any condition. Description of query is mentioned right above each query

protected void Page_Load(object sender, EventArgs e)
    {
        MydbDataContext db1 = new MydbDataContext();
        // ***Query to view all columns and rows ****
        //var query = db1.Products.Select(p => p);

        // ***Query to view all columns and rows order by product number****
        //var query = db1.Products.Select(p => p).OrderBy(p => p.ProductNumber);

        // ***Query to view two columns and a particular row where productid = 4***
        // var query = db1.Products.Where(p => p.ProductID == 4).Select(p => new { p.ProductID, p.ModifiedDate });

        // ***Query to view two columns and rows where p.ReorderPoint = 600 and p.SafetyStockLevel = 1000***
        //var query = db1.Products.Where(p => p
.ReorderPoint = 600 && p.SafetyStockLevel = 1000 ).Select(p => new { p.ProductID, p.ModifiedDate });

        GridView1.DataSource = query;
        GridView1.DataBind();
    }

* Last blog Link http://myaspdotnet35.blogspot.com/2009/01/linq-to-sql-select-example.html
This will guide you how to prepare for this blog. 

Available below is a snap from code to view code with color coding. 

No comments: