erasmo carlos
erasmo carlos

Reputation: 682

C# Entity Framework - Sort View when reading from it

I have a piece of code that gets the contents of a view and returns them in a list to populate a drop-down menu on the front end.

    public ActionResult GetMeetingList()
    {
        using (var context = new ClerkEntities())
        {
            var meetingList = context.vwGetMeetings.ToList();               

            return Json(new { data = meetingList }, JsonRequestBehavior.AllowGet);
        }           
    }

There are 4 columns in this view: [MeetingID], [MeetingName],[MeetingTitle],[MeetingDate]

I need to have the results sorted by MeetingDate.

How can I specify the OrderBy in this line?:

context.vwGetMeetings.ToList();   

Thank you for your help. Erasmo

Upvotes: 1

Views: 36

Answers (1)

oerkelens
oerkelens

Reputation: 5151

Context.vwGetMeetings.OrderBy(x => x.MeetingDate).ToList();

And if you want to order descending:

Context.vwGetMeetings.OrderByDescending(x => x.MeetingDate).ToList();

What you are using is Linq, which will give you more relevant results (Linq sort) than (Entity Framework sort).

Upvotes: 2

Related Questions