嗨说我有对象:
public class InvoiceLine
{
}
和
public class InvoiceHeader
{
public char Group { get; set; }
public List<InvoiceLine> InvoiceLines { get; set; }
}
为他们设置的数据如下:
var invoiceLine1 = new InvoiceLine();
var invoiceLine2 = new InvoiceLine();
var invoiceLine3 = new InvoiceLine();
var invoiceLine4 = new InvoiceLine();
var invoiceLine5 = new InvoiceLine();
var invoiceLine6 = new InvoiceLine();
var invoiceLine7 = new InvoiceLine();
var invoiceLine8 = new InvoiceLine();
var invoiceHeader1 = new InvoiceHeader { Group = 'A', InvoiceLines = new List<InvoiceLine> { invoiceLine1, invoiceLine2 } };
var invoiceHeader2 = new InvoiceHeader { Group = 'A', InvoiceLines = new List<InvoiceLine> { invoiceLine3, invoiceLine4 } };
var invoiceHeader3 = new InvoiceHeader { Group = 'B', InvoiceLines = new List<InvoiceLine> { invoiceLine5, invoiceLine6 } };
var invoiceHeader4 = new InvoiceHeader { Group = 'B', InvoiceLines = new List<InvoiceLine> { invoiceLine7, invoiceLine8 } };
var invoiceHeaders = new List<InvoiceHeader>
{
invoiceHeader1,
invoiceHeader2,
invoiceHeader3,
invoiceHeader4
};
我想要得到的是每个组的 invoiceLines 列表。
所以我想A组:
invoice1
, invoice2
,invoice3
和invoice4
对于 B 组:
invoice5
, invoice6
,invoice7
和invoice8
我得到的最远的是:
var invoiceLinesGroupA = invoiceHeaders.SelectMany(x => x.InvoiceLines);
据我所知,这将获得所有八个 invoiceLines。不知何故,我需要按组进行区分,以便为 groupA 获取那些,并为 groupB 做同样的事情。
有人能帮忙吗?