Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Preetee Gangoosirdar 56 posts 257 karma points
    Apr 13, 2017 @ 09:26
    Preetee Gangoosirdar
    0

    How to sort a List<dynamic> containing ExpandoObjects

    Hi everyone,

    Am using nested content package to list out the upcoming events. An event can be held in different place on different date.

    Grateful if someone can help me out on sorting a List

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using System.Globalization;
    @using System.Linq;
    @using System.Collections.Generic;
    @using System.Dynamic;
    
    
    @{ var rows = new List<dynamic>();}
    
    @{
        var  eventObj = new ExpandoObject()  as IDictionary<string, Object>;
        eventObj["expertTitle"] = @Html.Raw(workshopThemeName); 
        eventObj["workshopId"] = @Html.Raw(workshop.Id.ToString()); 
        eventObj["eventVenue"] = @Html.Raw(venue);                                                                  
        eventObj["imgSrc"] = @Html.Raw(img);                                                                    
        eventObj["pageLink"] = @Html.Raw(pageLink);                                                                 
        eventObj["altData"] = @Html.Raw(altData);                                                                   
        eventObj["imgCropper"] = @Html.Raw(imgCropper);                                                                 
        eventObj["eventDate"] = @Html.Raw(startingDate);                                                                    
        rows.Add(eventObj);
    
    }           
    

    The following is not working for sorting out based on the starting date of event

      foreach(dynamic  eventItem in rows.OrderBy(x => ((IDictionary<string, object>)x)["eventDate"])){
                                 @eventItem.expertTitle
                             }
    

    Thank you for your kind help and responses.

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Apr 13, 2017 @ 11:45
    Michael Latouche
    0

    Hi Preetee,

    It could be due to the way you store the value of startingDate into your object. Using Html.Raw will transform its value to a string.

    So, for example, if your startingDate is an actual DateTime object, the HTML.Raw will transform it to its default string value. Sorting on this aferwards might then not have the expected output because you are the sorting on the string representation and not on the actual date anymore.

    What you could do is either store the actual date object in your dictionary, or, if you want to go with string values, make sure that your date is transformed in a string that will be sortable as you wish, e.g. by giving a "yyyy/MM/dd" format. So something like

    eventObj["eventDate"] = @Html.Raw(startingDate.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture);     
    

    Hope this helps.

    Cheers,

    Michael.

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Apr 13, 2017 @ 11:50
    Michael Latouche
    1

    Hi again Preetee,

    An addition of my comment: keep also in mind that in your OrderBy

    x => ((IDictionary<string, object>)x)["eventDate"]
    

    actually gives an object back, so you are sorting on "object" type, and not on "string" or "DateTime" type. Therefore, it might be helpful to also make an extra cast to the type you want to sort on, something like this:

    x => (string)(((IDictionary<string, object>)x)["eventDate"])
    

    or

    x => (DateTime)(((IDictionary<string, object>)x)["eventDate"])
    

    Cheers,

    Michaël.

  • Preetee Gangoosirdar 56 posts 257 karma points
    Apr 13, 2017 @ 18:11
    Preetee Gangoosirdar
    100

    Hi Michael,

    Mille merci :)
    i got it working by doing the following : -

    var startingDate=  venueItem.HasProperty("eventStartingDate") && venueItem.HasValue("eventStartingDate")? venueItem.GetPropertyValue<string>("eventStartingDate") :null ;
    var dat =  DateTime.Parse(startingDate, CultureInfo.InvariantCulture); 
    

    I set my date in my object

       eventObj["eventDate"] = dat; 
       rows.Add(eventObj);
    

    In my loop , i was able to sort properly

    foreach(dynamic eventItem in rows.OrderBy(x => (DateTime)(((IDictionary<string, object>)x)["eventDate"])) ){
    @eventItem.expertTitle
    }
    

    Joyeux vendredi :)

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Apr 14, 2017 @ 07:07
    Michael Latouche
    0

    Hi Preetee,

    Glad you got it working!

    Joyeux vendredi également ;-)

Please Sign in or register to post replies

Write your reply to:

Draft