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