.OrderByDescending().ToList() - Where Order by is a Date that might be Null
.OrderByDescending().ToList() - Where Order by is a Date that might be Null
(OP)
Hey guys!
I hope all is well. I've been working on my first Windows Phone 8 app in my spare time. It is basically a Task/Todo app that consists of Task Lists that contain multiple Tasks. I can sort the Task Lists off the title and that is no issue, the issue comes when I want to sort the Tasks by Due Date.
The due date it stored at Google (Google Tasks) as: 10/01/2012 00:00:00 and when I pull it into my UI I parse it like:
And that works great. Here is what the function looks like without sorting:
Below is what is I have now - it returns errors when the x.due is null.
I have also tried the below and it doesn't like the ??
Should I be doing this sort somewhere else or is there a way I can capture the null and return MinValue/MaxValue?
I hope all is well. I've been working on my first Windows Phone 8 app in my spare time. It is basically a Task/Todo app that consists of Task Lists that contain multiple Tasks. I can sort the Task Lists off the title and that is no issue, the issue comes when I want to sort the Tasks by Due Date.
The due date it stored at Google (Google Tasks) as: 10/01/2012 00:00:00 and when I pull it into my UI I parse it like:
CODE --> C#
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var formatString = value as string; if (!string.IsNullOrEmpty(formatString)) { return DateTime.Parse(formatString).Date.ToShortDateString(); } return "No Due Date"; }
And that works great. Here is what the function looks like without sorting:
CODE --> C#
try
{
DateTime dateValue;
var o = JObject.Parse(response.Content);
var lists = new List<TaskItem>();
if (o != null && o["items"] != null)
{
lists =
o["items"].Select(
m => new TaskItem((string)m.SelectToken("id"),
(string)m.SelectToken("kind"),
((string)m.SelectToken("title")) ==
String.Empty
? "Empty"
: (string)
m.SelectToken("title"),
(string)m.SelectToken("notes"),
(string)m.SelectToken("parent"),
(string)
m.SelectToken("position"),
(string)m.SelectToken("update"),
(string)m.SelectToken("due"),
(string)m.SelectToken("deleted"),
(string)m.SelectToken("hidden"),
(string)m.SelectToken("status"),
(string)
m.SelectToken("selfLink"),
(string)
m.SelectToken("completed")
)).ToList();
}
var tasks = new List<TaskItem>();
tasks.Clear();
tasks.AddRange(lists);
var list = obj[1] as Action<List<TaskItem>>;
if (list != null) list(tasks);
}
Below is what is I have now - it returns errors when the x.due is null.
CODE --> C#
)).OrderByDescending(x=>DateTime.Parse(x.due)).ToList();
I have also tried the below and it doesn't like the ??
CODE --> C#
)).OrderByDescending(x=>DateTime.Parse(x.due) ?? DateTime.MinValue)).ToList();
Should I be doing this sort somewhere else or is there a way I can capture the null and return MinValue/MaxValue?
- Matt
"If I must boast, I will boast of the things that show my weakness"
RE: .OrderByDescending().ToList() - Where Order by is a Date that might be Null
x=> x.due != null ? DateTime.Parse(x.due) : DateTime.MinValue
RE: .OrderByDescending().ToList() - Where Order by is a Date that might be Null
The below is the code for sorting by date where the upcoming items are at the top - and if there is no due date they are put at the top:
CODE
Change Datetime.MinValue to Datetime.MaxValue and the "Nulls" are pushed to the bottom.
- Matt
"If I must boast, I will boast of the things that show my weakness"
RE: .OrderByDescending().ToList() - Where Order by is a Date that might be Null
RE: .OrderByDescending().ToList() - Where Order by is a Date that might be Null