Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.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# 分组_C#_.net_Linq_Ado.net - Fatal编程技术网

C# 分组

C# 分组,c#,.net,linq,ado.net,C#,.net,Linq,Ado.net,我不知道为什么我会遇到以下异常我如何才能转换为分组行?分组行是什么意思 快速回答:将代码更改为 foreach(var InvoiceHeader in InvoiceHeadercollection) linq代码生成了一个新类,您可以这样想: class AutoGeneratedClass : IEnumerable<DataRow> { public string Key { get; } // The InvoiceAccount field } 类自动生成

我不知道为什么我会遇到以下异常我如何才能转换为分组行?分组行是什么意思


快速回答:将代码更改为

foreach(var InvoiceHeader in InvoiceHeadercollection)
linq代码生成了一个新类,您可以这样想:

class AutoGeneratedClass :  IEnumerable<DataRow>
{
    public string Key { get; }  // The InvoiceAccount field
}
类自动生成类:IEnumerable
{
公共字符串键{get;}//InvoiceAccount字段
}

(它不是“AutoGeneratedClass”;我只是使用它,所以它遵循我们知道的语法)。因为这个自动生成的类没有名称,所以您必须使用“var”。

看看您的LINQ查询,您使用的是
group by
-
InvoiceHeaderCollection
不是数据行的集合,它是一个
IGROUP
,每个不同的“发票帐户”都是一个不同的组。
-你想对分组做什么?或者您是否打算按发票帐户订购行

InvoiceHeaderCollection
是组的集合,这些组类似于:

      Key               Value
 invoice account1      datarow1 //the value is a `DataRow`
                       datarow2
                       datarow4
 invoice account2      datarow3
 invoice account3      datarow5 
因此,我认为您需要:

foreach(var g in InvoiceHeaderCollection)
{
    DataRow[] rows = g.ToArray();   //to get all the DataRows in the group
    //maybe another loop through the rows
}

我很惊讶,即使是编译。然后我将获得on字段(…)