Sorry to open another post.
I asked in the previous post but I can't get the solution for my problem.
`
var departments = stops
.Where(stop => stop.InitDate != null)
.SelectMany(stop => new[] { Month = stop.InitDate.Month, Year = stop.InitDate.Year, Duration = stop.Duration })
.GroupBy(dt => new { dt.Month, dt.Year })
.OrderBy(g => g.Key.Month)
.ThenBy(g => g.Key.Year)
.Select(g => new
{
Key = g.Key.Month,
Año = g.Key.Year,
Duration = g.Sum(v => v.Duration),
Count = g.Count()
});
`
This is de final solution to my problem, but, when I use this in my code, I have some problems.
If I don't declare the variables "Month, Year, Duration", I get an error in:
.SelectMany(stop => new[] { Month = stop.InitDate.Month, Year = stop.InitDate.Year, Duration = stop.Duration })
But I do not know what kind of data they are month and year because if I declare it how integer, I get an error in .GroupBy(dt => new { dt.Month, dt.Year })
, because the compiler recognizes dt as integer.
I tried to declare Month and Year as integer and put in the .GroupBy this:
.GroupBy(dt => new { Month, Year })
but it is not correct...
Thank you in advance
Raúl