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
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"])
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
}
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
The following is not working for sorting out based on the starting date of event
Thank you for your kind help and responses.
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
Hope this helps.
Cheers,
Michael.
Hi again Preetee,
An addition of my comment: keep also in mind that in your OrderBy
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:
or
Cheers,
Michaël.
Hi Michael,
Mille merci :)
i got it working by doing the following : -
I set my date in my object
In my loop , i was able to sort properly
Joyeux vendredi :)
Hi Preetee,
Glad you got it working!
Joyeux vendredi également ;-)
is working on a reply...