How do you show this list ? Could it be that you use a control that provides its own default ordering ?
BTW show always complete lines (you don't assign the resulting list ?), you have the same result if you are using just myList=mappedTasks.MwuTaskHistories.OrderBy(x
=> x.ActionedDate).ToList(); ?
Ok and about the assignement ? Do you assign this list somewhere or then you use .Local ? If not what you assign this list here and then put a breakpoint and check the ordering at this point. You are 100% sure that the ordered list is what you are showing
here ?
I'm trying to understand if the issue is the list returned by this particular instruction is really not sorted or if it is sorted and that for some reason (ie this is not THIS list that is used or you are doing something else before consuming it) it is changed
again before being consumed.
its really not sorted. In my controller directly after the the line with the OrderBy clause I return mappedTasks. placing a breakpoint on the return line and looking at the mappedTaskes.MwuTaskHistories using intellisense shows them in the original order
and this isthe order that gets put put by the view
This is LINQ to SQL ? What is the underlying db ? I found surprising you used Ticks as x=>x.ActionedDate should provide the same ordering.
I made a quick test with EF/SQL Server, translating Ticks to a SQL Server is not supported but x=>x.ActionedDate just works fine... (I'm writing a non ordered list and an ordered list on the console and the ordering is correct). Not sure where to look at.
Have you tried just x=>x.ActionedDate ? Could it be something like the ORDER clause being silently dropped if the sort expression can't be translated to run against whatever db you are using ? Weird.
Edit: "I return mappedTasks" really ? What it is ? Isn't it your DbContext or do you mean that you return the list produced by the whole expression. Could you show the relevant part. It looks like you are calling A.B.CreateList(); and then return A. I expected
something like return A.B.ToList();
The original code just use x=>x.ActionDate, and becasue that didn't work I started to try using some of the properties off the value, but they just done work. We are obtaining the original dataset via Entity Framework. We are using 4.1 and we are using SQL
Server 2008. Below is the whole function to hopefully make it a bit clearer.
You will notice I have placed a line above my OrderBy line and sorting into a variable 'q'. I placed this here as a test and if I alternate this line between ascending and descending 'q' has the values in the correct order????
private MwuTaskDetailViewModel PrepareTaskDetailViewModel(int id)
{
var taskResults = _tasksService.GetTaskDetailsForTaskId(id);
var taskHistories = taskResults.WorkflowHistories.ToList();
var mappedTasks = _mapper.Map<PanelManagementModels.TaskList.MWUTask, MwuTaskDetailViewModel>(taskResults);
var mappedTaskHistories = _mapper.Map<List<PanelManagementModels.TaskList.WorkflowHistory>, List<MwuTaskHistory>>(taskHistories);
mappedTasks.MwuTaskHistories = mappedTaskHistories;
// mappedTasks.Notes = mappedNotes.Select(m=>m.Details).ToList();
//Get values that cannot be mapped.
var taskHistory = taskHistories.SingleOrDefault(m => m.OutcomeChosenID == 10);
if (taskHistory != null)
{
var completedUser = _tasksService.GetTaskUserById(taskHistory.TaskActionedByID);
mappedTasks.ClosedBy = completedUser.Name;
}
foreach (var mappedTaskHistory in mappedTaskHistories)
{
var note = _tasksService.GetWorkflowNotes(id, mappedTaskHistory.WorkflowHistoryID);
if (note != null)
{
mappedTaskHistory.Notes.Add(note.Details);
}
var actionedUser = _tasksService.GetTaskUserById(mappedTaskHistory.ActionedByID);
mappedTaskHistory.ActionedBy = actionedUser.Name;
}
if(taskResults.Notes.Any())
{
foreach (var item in taskResults.Notes)
{
var notes = new List<string> {item.Details};
mappedTasks.MwuTaskHistories.Add(new MwuTaskHistory()
{
TaskType = item.MWUTask.TaskType.Description,
ActionedBy = item.MWUUser.FirstName,
ActionedDate = item.CreatedDate,
Notes = notes
});
}
}
var q = from m in mappedTasks.MwuTaskHistories orderby m.ActionedDate ascending select m;
mappedTasks.MwuTaskHistories.OrderBy(x => x.ActionedDate).ToList();
return mappedTasks;
}
Member
202 Points
552 Posts
Ordering by datetime is ignored
May 22, 2014 07:44 AM|billcrawley|LINK
Hi All,
I have the following statement:
MwuHistories is declared as IList<MwuTaskHistory> on the mappedTasks object
It doesn't matter how I try to use the OrderBy on this column it get's ignored and returns in the default order.
All-Star
48320 Points
18012 Posts
Re: Ordering by datetime is ignored
May 22, 2014 07:49 AM|PatriceSc|LINK
Hi,
How do you show this list ? Could it be that you use a control that provides its own default ordering ?
BTW show always complete lines (you don't assign the resulting list ?), you have the same result if you are using just myList=mappedTasks.MwuTaskHistories.OrderBy(x => x.ActionedDate).ToList(); ?
Member
202 Points
552 Posts
Re: Ordering by datetime is ignored
May 22, 2014 08:45 AM|billcrawley|LINK
The view (MVC3) uses Knockout and we are using a standard html table, but binding the data to a Knockout element :
All-Star
48320 Points
18012 Posts
Re: Ordering by datetime is ignored
May 22, 2014 09:21 AM|PatriceSc|LINK
Ok and about the assignement ? Do you assign this list somewhere or then you use .Local ? If not what you assign this list here and then put a breakpoint and check the ordering at this point. You are 100% sure that the ordered list is what you are showing here ?
I'm trying to understand if the issue is the list returned by this particular instruction is really not sorted or if it is sorted and that for some reason (ie this is not THIS list that is used or you are doing something else before consuming it) it is changed again before being consumed.
Member
202 Points
552 Posts
Re: Ordering by datetime is ignored
May 22, 2014 10:34 AM|billcrawley|LINK
its really not sorted. In my controller directly after the the line with the OrderBy clause I return mappedTasks. placing a breakpoint on the return line and looking at the mappedTaskes.MwuTaskHistories using intellisense shows them in the original order and this isthe order that gets put put by the view
All-Star
48320 Points
18012 Posts
Re: Ordering by datetime is ignored
May 22, 2014 11:47 AM|PatriceSc|LINK
This is LINQ to SQL ? What is the underlying db ? I found surprising you used Ticks as x=>x.ActionedDate should provide the same ordering.
I made a quick test with EF/SQL Server, translating Ticks to a SQL Server is not supported but x=>x.ActionedDate just works fine... (I'm writing a non ordered list and an ordered list on the console and the ordering is correct). Not sure where to look at. Have you tried just x=>x.ActionedDate ? Could it be something like the ORDER clause being silently dropped if the sort expression can't be translated to run against whatever db you are using ? Weird.
Edit: "I return mappedTasks" really ? What it is ? Isn't it your DbContext or do you mean that you return the list produced by the whole expression. Could you show the relevant part. It looks like you are calling A.B.CreateList(); and then return A. I expected something like return A.B.ToList();
Member
202 Points
552 Posts
Re: Ordering by datetime is ignored
May 22, 2014 12:02 PM|billcrawley|LINK
The original code just use x=>x.ActionDate, and becasue that didn't work I started to try using some of the properties off the value, but they just done work. We are obtaining the original dataset via Entity Framework. We are using 4.1 and we are using SQL Server 2008. Below is the whole function to hopefully make it a bit clearer.
You will notice I have placed a line above my OrderBy line and sorting into a variable 'q'. I placed this here as a test and if I alternate this line between ascending and descending 'q' has the values in the correct order????
All-Star
48320 Points
18012 Posts
Re: Ordering by datetime is ignored
May 22, 2014 03:59 PM|PatriceSc|LINK
Ok, so it seems the issue is that you do nothing with the ordered list generated by your expression. What if you try :
mappedTasks.MwuTaskHistories=mappedTasks.MwuTaskHistories.OrderBy(x => x.ActionedDate).ToList();
to assign back the ordered list to the MwUTaskHistories list exposed by your model ?