I have a list that comes from a database and want to sort the DateofCreation
variable from that list. It's datatype is in string.
How do I go about it?
I went about doing what was instructed in this site here
and revised it to fit my code, the error is
Error CS0834 A lambda expression with a statement body cannot be converted to an expression tree
var orderedList3 = collectionRecords.OrderByDescending(x =>
{
DateTime dt;
if (!DateTime.TryParse(x.DateOfCreation, out dt)) return DateTime.MaxValue;
return dt;
});
collectionRecords
comes from
public class CollectionRecords
{
[Key]
...
...
public string AuthorSource { get; set; }
public string DateOfCreation { get; set; }
public string VolumeIssueNo { get; set; }
...
}
Sample Input:
- undated
- 1991 May 8
- march 2012
- various dates
- Apr 8 2018
Expected Output:
- Apr 8 2018
- March 2012
- 1991 May 8
- various dates
- undated