I dont know why i am 开发者_StackOverflowgetting the following exception how can I cast into grouping Row ? What does grouping row means
Quick answer: change the code to
foreach(var InvoiceHeader in InvoiceHeadercollection)
The linq code has generated a new class, which you can think of like this:
class AutoGeneratedClass : IEnumerable<DataRow>
{
public string Key { get; } // The InvoiceAccount field
}
(It's not called "AutoGeneratedClass"; I'm just using that so it follows the syntax we know). Because there's no name for this automatically generated class, you have to use "var".
Look at your LINQ query, you are using group by
- InvoiceHeaderCollection
is not a collection of data rows, it's an IGrouping<DataRow>
, a different group for each different "Invoice account".
- what did you want to do with the grouping? Or did you intend to order your rows by Invoice account?
InvoiceHeaderCollection
is a collection of groups, the groups is something like:
Key Value
invoice account1 datarow1 //the value is a `DataRow`
datarow2
datarow4
invoice account2 datarow3
invoice account3 datarow5
So I think you need:
foreach(var g in InvoiceHeaderCollection)
{
DataRow[] rows = g.ToArray(); //to get all the DataRows in the group
//maybe another loop through the rows
}
精彩评论