我正在使用linq对数据进行分组,您可以从下面的代码中看到,在分组之后,我想对组数据进行交互并添加到另一个表中。
但是我只得到InvoiceHeader。关键,我不知道如何获得其他字段
DataTable VendorInvoiceStagingTable;
DataTable VendorInvoiceTable;
VendorInvoiceStagingTable = new Program().ReadExcelFile(@"C:Usershuzaifa.gainDesktopVendor invoice import - sample data set.xlsx", "Sheet2");
var InvoiceHeadercollection = from row in VendorInvoiceStagingTable.AsEnumerable()
group row by row.Field<string>(VendInvoice.Number) into grp
select grp;
VendorInvoiceTable = new Program().CreateHeader();
foreach (var InvoiceHeader in InvoiceHeadercollection)
{
VendorInvoiceTable.Rows.Add(InvoiceHeader.Key, .....
}
由于InvoiceHeader
是实现IEnumerable
的IGrouping
,您可以简单地枚举组中的项目,因此您可以使用foreach
循环:
foreach (var InvoiceHeader in InvoiceHeadercollection)
{
foreach(var row in InvoiceHeader)
VendorInvoiceTable.Rows.Add(row);
}