Showing posts with label Select. Show all posts
Showing posts with label Select. Show all posts

21 February 2014

LINQ Select Query with Order by

LINQ Select Query with Order by:

Below LINQ query will get the Students details from table with Order by

var students = from stu in sqlObj.tbl_students
                      orderby stu.Id descending
                       select new
                       {
                           stu.Id,
                           stu.Name,
                       };

Table data

Asp.net Code
<form id="form1" runat="server">
    <div>
    <center>
    <asp:GridView ID="gv1" runat="server"></asp:GridView>
    </center>
    </div>
    </form>

C#.net Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Select_With_order_by_ : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MydbDataContext sqlObj = new MydbDataContext();
        var students = from stu in sqlObj.tbl_students
                      orderby stu.Id descending
                       select new
                       {
                           stu.Id,
                           stu.Name,
                       };
        gv1.DataSource = students;
        gv1.DataBind();
    }
}

OutPut