Having the following interface:
public class foo
{
[Key]
public int Id { get; set; }}
public string Notes { get; set; }
public DateTime? Timestamp { get; set; }
}
Using data context such as: await context.foos.ToListAsync() returns a full list of entries. The goal I want to achieve is a restructured JSON object from the same response as below:
[
{
year: YYYY,
count: 0, // <- total `foos` in year
months: [
{
month: 'January',
count: 0, // <- total `foos` in month
days: [
{
day: 0,
count: 0, // <- total `foos` in day
foos: [
{
Id: 0,
Notes: '...',
Timestamp: '...'
}
]
}
]
}
]
}
]
I was able to achieve this with multiple nested for loops. However, this seems like a very bad approach since nested loops increase process time exponentially.
Is there a possible way to achieve this with LINQ? Or maybe a more efficient solution?