我发现这篇文章,我需要类似的东西
我需要按月和年分组,但同时我需要主要元素的属性,我无法访问
var departments = stops
.SelectMany(x => new[] { x.InitDate.Month, x.InitDate.Year }
.Where(dt => dt != null).Select(dt => x.InitDate))
.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 = 0,
Count = g.Count()
});
我需要访问“stops.Duration”,但如果我这样做:.SelectMany(x => new[] { x.InitDate.Month, x.InitDate.Year, x.Duration }
它没有按月分组我
有人能帮助我吗?
对不起我的英文,非常感谢你
答案 0 :(得分:0)
此代码应该:
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()
});
选择持续时间,月份和年份组,并使用分组结果中的持续时间总和。
答案 1 :(得分:0)
int Month = 0, Year= 0, Duration = 0;
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 { Month, 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 => Duration),
Count = g.Count()
});
对我来说,这是最终的解决方案