Chathz
Chathz

Reputation: 743

Sort table according to Date , using Linq query in mvc 4

I just created mvc 4 application. In that application I'm having university table.I want to sort this table data according to Create Date(datetime) column

enter image description here

this is my Ling query and controller method for sort and list this table

    [System.Web.Mvc.Authorize]
    public ActionResult U_Index(string HEI_ID = null)
    {
        try
        {

            var Institutes = from university in db.Institutes
                             where university.HEI_ID == HEI_ID
                             select university;

            return View(db.Institutes.OrderByDescending(i => i.Create_Date).ToList());
        }
        catch (Exception ex)
        {

            return View();
        }
    }

but this isn't sorting by create data , Just want know the thing I've missed

Upvotes: 2

Views: 1320

Answers (2)

Gaurav
Gaurav

Reputation: 154

In the return statement you are using a property of the db variable instead of the Institutes variable you just defined above.

return View(db.Institutes.OrderByDescending(i => i.Create_Date).ToList());

change this to use the new variable:

return View(Institutes.OrderByDescending(i => i.Create_Date).ToList());

Edit: Reworded the question to a valid answer. The details provided a valid answer, but was worded as a question.

Upvotes: 2

Steve
Steve

Reputation: 9551

Try ordering in the Linq query:

var Institutes = from university in db.Institutes
                 where university.HEI_ID == HEI_ID
                 orderby university.Create_Date descending
                 select university;

Upvotes: 0

Related Questions