Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当您有两个不同的分组时,如何使用groupBy?_C#_Linq_Group By - Fatal编程技术网

C# 当您有两个不同的分组时,如何使用groupBy?

C# 当您有两个不同的分组时,如何使用groupBy?,c#,linq,group-by,C#,Linq,Group By,我有这个样本数据 1/14/2012,5,Gas 1/15/2012,5,Gas 1/16/2012,5,Gas 1/17/2012,5,Gas 1/18/2012,5,Gas 1/19/2012,5,Gas 1/20/2012,5,Gas 1/21/2012,5,Gas 1/22/2012,5,Gas 1/23/2012,5,Gas 1/24/2012,5,Gas 1/25/2012,5,Gas 1/26/2012,5,Gas 1/27/2012,5,Gas 1/28/2012,5,Gas 1

我有这个样本数据

1/14/2012,5,Gas
1/15/2012,5,Gas
1/16/2012,5,Gas
1/17/2012,5,Gas
1/18/2012,5,Gas
1/19/2012,5,Gas
1/20/2012,5,Gas
1/21/2012,5,Gas
1/22/2012,5,Gas
1/23/2012,5,Gas
1/24/2012,5,Gas
1/25/2012,5,Gas
1/26/2012,5,Gas
1/27/2012,5,Gas
1/28/2012,5,Gas
1/29/2012,5,Gas
1/30/2012,5,Gas
1/31/2012,5,Gas
02/01/2012,5,Gas
有时我想按“月”和“年”分组有时我只想按“年”分组

结果

Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
Month: 0, Year: 2012
当它应该只有一条记录,并且所有记录都已分组时。因为他们都是从2012年开始的

编辑

斯科特·里皮-我试过你给我的东西,但还是没用

 groupTransactions = filterdTransactions.GroupBy(
                            // Key selector:
                            x => x.TransactionDate.Year,
                            // result selector:
                            (year, items) => new TransactionsGroupedByMonthYear{ Year = year, Month = 0, Transactions = items});


public class TransactionsGroupedByMonthYear
{
    public int Month { get; set; }
    public int Year { get; set; }
    public IList<TransactionDto> Transactions { get; set; }

}

Error   1   Cannot convert lambda expression to type 'System.Collections.Generic.IEqualityComparer<int>' because it is not a delegate type      
groupTransactions=filterdTransactions.GroupBy(
//按键选择器:
x=>x.TransactionDate.Year,
//结果选择器:
(年,项目)=>new TransactionGroupedByMonthyear{year=year,Month=0,Transactions=items});
公共类事务按Monthyear分组
{
公共整数月{get;set;}
公共整数年{get;set;}
公共IList事务{get;set;}
}
错误1无法将lambda表达式转换为类型“System.Collections.Generic.IEqualityComparer”,因为它不是委托类型
编辑2

我刚注意到你有一个项目。toList()

现在我明白了

Error   1   Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<CCRecomendator.Framework.Domain.DTO.TransactionsGroupedByMonthYear>' 
                                        to 'System.Collections.Generic.IEnumerable<System.Linq.IGrouping<CCRecomendator.Framework.Domain.DTO.TransactionsGroupedByMonthYear,CCRecomendator.Framework.Domain.DTO.TransactionDto>>'. An explicit conversion exists (are you missing a cast?)
错误1无法隐式转换类型“System.Collections.Generic.IEnumerable”
至“System.Collections.Generic.IEnumerable”。存在显式转换(是否缺少强制转换?)
所以我看到的问题是,它返回一个不同的类型。我需要使用“月”和“年”开关分组代码来返回相同的数据类型(否则我只会使用匿名类型)。

为什么GroupBy不工作
transactiongroupedbymonthyear
不会覆盖相等运算符,因此
GroupBy
将使用参考比较——换句话说,比较时没有两个
transactiongroupedbymonthyear
相等

最简单的解决方案是执行第一个示例中的操作。。。改用匿名对象。匿名对象自动覆盖相等运算符,非常适合
GroupBy
操作

否则,您必须重写
TransactionGroupedByMonthyear
类中的
.Equals
.GetHashCode
方法

你为什么使用GroupBy? 您是否确实需要每年的分组项目列表?如果你只是想得到一个不同年份的列表,你不应该使用
GroupBy
,你应该使用
distinct

根据
TransactionGroupedByMonthyear
的名称判断,我看到了
,但我猜您应该也有
交易
属性。如果这是真的,那就继续读下去

您的代码似乎正在尝试输出存储在
TransactionGroupedByMonthyear
中的分组项目列表 为此,应使用以下重载:

基本上,此重载允许您对每个匹配的组执行某些操作,例如创建一个
TransactionGroupedByMonthyear

groupTransactions = filterdTransactions.GroupBy(
    // Key selector:
    x => new { x.Year },
    // result selector:
    (key, items) => new TransactionsGroupedByMonthYear{ Year = key.Year, Month = 0, Transactions = items.ToList()})
);
回复:编辑2 您在编辑2中看到错误的原因是因为以下行:

IEnumerable<IGrouping<TransactionsGroupedByMonthYear, TransactionDto>> groupTransactions = null;
IEnumerable groupTransactions=null;
应该是:

IEnumerable<TransactionsGroupedByMonthYear> groupTransactions = null;
IEnumerable groupTransactions=null;

您只是将其声明为错误的类型。

请包含一些关于
TransactionGroupedByMonthyear
的信息。它是什么,如何使用?你确定这是正确的代码吗?除了
之外,我还希望看到
交易
属性。可能不会。就像我说的,这是别人让我做的。所以我尝试了它,但它不起作用。所以我需要添加一个TransactionDtos what is Items的集合?好的,我尝试了它,但我一直得到一个错误(请参阅我的编辑)。我希望使用匿名类型,但正如您所见,我有一个switch语句,在某些情况下,我希望按月份和年份分组,有时只是按年份分组,但我希望稍后返回transactionsDto,但我不能使用匿名类型。是的,您需要将
TransactionDto
s的集合添加到
transactiongroupedbymonthyear
对象中。
IEnumerable<IGrouping<TransactionsGroupedByMonthYear, TransactionDto>> groupTransactions = null;
IEnumerable<TransactionsGroupedByMonthYear> groupTransactions = null;