假设您有以下简单对象:
class Order { public Customer[] Customers { get; set; } } class Customer { public SaleLine[] SaleLines { get; set; } } class SaleLine { public Tax[] MerchTax { get; set; } public Tax[] ShipTax { get; set; } } class Tax { public decimal Rate { get; set; } public decimal Total { get; set; } }
有了这些对象,我希望能够获得整个订单上使用的所有独特税率的清单,包括商品和运费税率.
var TaxRates = MyOrder.Customers .SelectMany(customer => customer.SaleLines) .SelectMany(saleline => saleline.MerchTax) .GroupBy(tax => tax.Rate) .Select(tax => tax.First().Rate
如何获得包含商品和运费的唯一税率列表的列表?
解决方法
听起来你想要这个:
var TaxRates = MyOrder.Customers .SelectMany(customer => customer.SaleLines) .SelectMany(saleline => saleline.MerchTax.Concat(saleline.ShipTax)) .GroupBy(tax => tax.Rate) .Select(group => group.Key);
基本上,更改是对Concat的调用,它将两个序列连接在一起.