I have sql database that has a DateTime field. When I query with LINQ to Object I get a date in one format "20/01/2022 12:00:00 AM", but if SelectMany (LINQ to Entinty) get a different format "Jan 20 2021 12:00AM".
The desired output is the flat data structure that is acheived by the foreach after a select query or SelectMany query.
Why do i get differnt output from same data and what is a good way to make consisent depending on using SelectMany or Select.
How can I fix this to get the same format in one place?
Which method is better? Not use SelectMany?
Here is the code:
var a = db.Tasks.Include("Actions.ProgressUpdate.State")
.Where(p => p.ProjectId == ProjectId && p.Actions.Count>0)
.ToList();
var b = db.Tasks.Include("Actions.ProgressUpdate.State")
.Where(p => p.ProjectId == ProjectId && p.Actions.Count>0);
//object list gives date as "20/01/2022 12:00:00 AM"
var objectList = new List<dynamic>();
foreach (var item in a)
{
foreach (var action in item.Actions)
{
aList.Add(new
{
TaskId = item.Id,
date = action.CurrentDueDate.ToString()
});
}
}
//entinty gives date as "Dec 9 2021 4:31PM"
var entityList = b
.SelectMany(p => p.Actions,
(parent, child) => new
{
TaskId = parent.Id,
date = child.CurrentDueDate.ToString()
});