C# .用C选择many#

C# .用C选择many#,c#,linq,C#,Linq,我找到了这篇文章,我需要类似的东西 我需要按月份和年份分组,但同时我需要一个主元素的属性,我无法访问该属性 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.

我找到了这篇文章,我需要类似的东西

我需要按月份和年份分组,但同时我需要一个主元素的属性,我无法访问该属性

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}

它不按月份和年份对我进行分组

有人能帮我吗

对不起,我的英语太差了,非常感谢您

此代码应该可以:

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() 
    });
它选择持续时间、按月分组和按年分组,并使用分组结果中的持续时间总和。

此代码应执行以下操作:

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() 
    });
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() 
    });
它选择持续时间、按月分组和按年分组,并使用分组结果中的持续时间总和

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() 
    });
对我来说,这是最终的解决方案



对我来说,这是最终的解决方案

你需要对持续时间做什么?平均/最大/最小/总和/计数?我需要对持续时间进行总和主题外的注释,永远不要在代码中使用特殊字符。我正在看名字Año(西班牙语中的年份).帮你自己一个忙,不要这样做。你需要对持续时间做什么?平均/最大/最小/总和/计数?我需要对持续时间进行总和主题外的注释,永远不要在代码中使用特殊字符。我正在查看名称año(西班牙语中的年份)。帮你自己一个忙,不要这样做。你应该解释问题是什么-
其中
应用于
SelectMany
的参数,而不是stops,返回意外的结果。我有一个问题,因为当我这样做时,año=g.Sum(v=>v.Duration)Duration无法识别。我还认为你需要一个括号“”还有一个问题。G是日期时间,不是停止类型。否。这是一种匿名类型。如果我使用此代码,第一个问题是在.SelectMany(x=>new[]{Month=stop.InitDate.Month,Year=stop.InitDate.Year,Duration=stop.Duration}。我不知道问题出在哪里。您应该解释问题出在哪里-
应用于
SelectMany
的参数,而不是stops,返回意外的结果。我有一个问题,因为当我这样做时,año=g.Sum(v=>v.Duration)Duration无法识别。我还认为您需要一个括号“”还有一个问题。G是日期时间,不是停止类型。否。这是一种匿名类型。如果我使用此代码,第一个问题是在.SelectMany(x=>new[]{Month=stop.InitDate.Month,Year=stop.InitDate.Year,Duration=stop.Duration}。我不知道有什么问题,代码不分组任何东西,因为它是在常量上分组的。问题在GroupBy中?如果我写“new{dt.Month,dt.Year}”,编译器会指示找不到整数的月份(stop.InitDate.Month是整数)该代码没有对任何内容进行分组,因为它在常量上分组。问题出在GroupBy中?如果我写“new{dt.Month,dt.Year}”,编译器会指示找不到整数的月份(stop.InitDate.Month是整数)