I am trying to sort my records out in the jQuery jTable but it is not sorting the records. I am getting records from an excel file.
Here's the sorting bit of the code that I am trying to sort the records:
if (sorting != null)
{
daa.OrderByDescending(i => i.Date);
}
Here's my full code for your inspection:
public JsonResult TopPlayedInVenueList1(string sorting, string StartDate = "", string EndDate = "", int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
{
try
{
if (Request.IsAuthenticated == true)
{
string Path = @"C:\\5Newwithdate-1k.xls";
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= '" + Path + "';Extended Properties=" + (char)34 + "Excel 8.0;IMEX=1;" + (char)34 + "");
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
con.Close();
System.Data.DataTable data = new System.Data.DataTable();
da.Fill(data);
List<TopPlayed> daa = new List<TopPlayed>();
foreach (DataRow p in data.Rows)
{
TopPlayed top = new TopPlayed()
{
TrackID = Convert.ToInt32(p.Field<double>("TrackID")),
Date = p.Field<DateTime>("DateTimes"),
TrackName = p.Field<string>("TrackName"),
ArtistName = p.Field<string>("ArtistName"),
Times = Convert.ToInt32(p.Field<double>("Times"))
};
daa.Add(top);
}
var listOrder = daa.Where(i => i.Date >= Convert.ToDateTime(StartDate) && i.Date <= Convert.ToDateTime(EndDate)).ToList();
var newlist = listOrder.ToList().GetRange(jtStartIndex, jtPageSize);
if (!string.IsNullOrWhiteSpace(sorting))
{
newlist = listOrder.OrderByDescending(i => i.Date);
}
return Json(new { Result = "OK", Records = newlist, TotalRecordCount = listOrder.ToList().Count });
Before posting, I tried this and this examples but none of them worked.
To clarify, I am getting data from an excel file so that makes the List
IEnumerable
and not IQueryable
right? Could you please kindly help out on this sorting issue. Thanks in advance.
Edit: Here's the updated code; Konrad
if (sorting != null)
{
daa.OrderBy(i => i.Date);
}
var result = daa.OrderBy(i => i.Date);