locked
Ordering by datetime is ignored RRS feed

  • Question

  • User-963208184 posted

    Hi All,

    I have the following statement:

     mappedTasks.MwuTaskHistories.OrderBy(x => x.ActionedDate != null ? x.ActionedDate.Value.Ticks : 0).ToList();

    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.

    Thursday, May 22, 2014 7:44 AM

Answers

  • User753101303 posted

    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 ?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 22, 2014 3:59 PM

All replies

  • User753101303 posted

    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(); ?

    Thursday, May 22, 2014 7:49 AM
  • User-963208184 posted

    The view (MVC3) uses Knockout and we are using a standard html table, but binding the data to a Knockout element :

    <table style="width: 100%; border-spacing: 0;">
                        <thead class="accordionHeader" style="width: 100%;">
                            <tr>
                                <td colspan="2">
                                    Task/ActionType
                                </td>
                                <td>
                                    Due
                                </td>
                                <td>
                                    Actioned
                                </td>
                                <td>
                                    Actioned By
                                </td>
                                <td>
                                    SLA Fail
                                </td>
                                <td colspan="4">
                                    Note
                                </td>
                            </tr>
                        </thead>
                        <tbody class="generalMessage" data-bind="foreach: TaskPanelHistories">
                            <tr>
                                <td colspan="2">
                                    <span data-bind="text: TaskType"></span>
                                </td>
                                <td>
                                    <span data-bind="DateTime: DueDate"></span>
                                </td>
                                <td>
                                    <span data-bind="DateTime: ActionedDate"></span>
                                </td>
                                <td>
                                    <span data-bind="text: ActionedBy"></span>
                                </td>
                                <td>
                                    <span data-bind="text: SLAFail"></span>
                                </td>
                                <td colspan="4" data-bind="foreach:Notes">
                                    <span data-bind="text: $data"></span>
                                </td>
                            </tr>
                        </tbody>
                    </table>

    Thursday, May 22, 2014 8:45 AM
  • User753101303 posted

    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.

    Thursday, May 22, 2014 9:21 AM
  • User-963208184 posted

    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

    Thursday, May 22, 2014 10:34 AM
  • User753101303 posted

    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();

     

     

    Thursday, May 22, 2014 11:47 AM
  • User-963208184 posted

    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;
            }

    Thursday, May 22, 2014 12:02 PM
  • User753101303 posted

    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 ?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 22, 2014 3:59 PM