Dynamic Sort in Linq Statement

Last post 11-19-2008 6:22 AM by deepakkapoor.net. 1 replies.

Sort Posts:

  • Dynamic Sort in Linq Statement

    11-18-2008, 6:10 PM
    • Member
      point Member
    • lpage
    • Member since 10-22-2008, 3:28 PM
    • Posts 3

    Does anyone have any examples of how to include a dynamic sort field into a linq statement? There are lots of examples online on how to sort on a supplied field name, but the work is done after grabbing the data from the database. I actually want to grab the data from the database already sorted. Here are some quick details:

     1) The field name is supplied by a grid, so compile time I don't know what the field name will be. This example addresses that, but it does the processing after grabbing the data: http://www.onedotnetway.com/dynamic-sort-with-linq/

     2) I have a bunch of data, so i have a linq statement that grabs only the first 10 records... On top of that I need to inject the supplied field name so I can sort or group by it. The field is only determined during run time.

    var query = from v in db.myTable where v.SiteID == siteID && v.VisitDate >= dataDate && v.VisitDate <= dataDate.AddDays(1)

    select new VisitListDataRowOld

    {

    visitornumber=v.VisitorNumber,

    visitdate=
    DateUtil.GetDateString(v.VisitDate),

    };

    data.visits = query.Skip(startRowIndex).Take(maximumRows).ToList();

    data.recordcount = query.Count();

    How can I pull this data from the database already sorted?

    Thanks!

    Levi

    Filed under:
  • Re: Dynamic Sort in Linq Statement

    11-19-2008, 6:22 AM
    Answer
    • Member
      18 point Member
    • deepakkapoor.net
    • Member since 04-22-2008, 12:32 AM
    • Sydney, Australia
    • Posts 4
    Hi Levi,

    I have tried to answer your question at http://www.onedotnetway.com/dynamic-sort-with-linq-to-sql/

    I hope this helps.

    Regards,

    Filed under: ,
Page 1 of 1 (2 items)