1.List<T>去重,并把里面的值加起来
List<OrderPaymentPrintOutputDto> result = new List<OrderPaymentPrintOutputDto>();
while (payList.Count > 0)
{
OrderPaymentPrintOutputDto m = payList[0];
payList.RemoveAt(0);
List<OrderPaymentPrintOutputDto> listB = new List<OrderPaymentPrintOutputDto>();
foreach (OrderPaymentPrintOutputDto item in payList)
{
if (item.PaymentName.Equals(m.PaymentName))
{
m.PayAmount += item.PayAmount;
}
else
{
listB.Add(item);
}
}
payList = listB;
result.Add(m);
}
2.List中相同的项,把值加起来去重
var payments = new CustomPaymentTypeService(_WorkContext).AllPaymentList(true);
List<OrderPaymentPrintOutputDto> payList = new List<OrderPaymentPrintOutputDto>();
foreach (var item in _OrderPaymentRepository.Get(p => p.CompID == _WorkContext.CompID && p.OrderID == model.Id && p.PayAmount > 0, null, false))
{
var payInfo = payments.FirstOrDefault(p => p.PaymentCode == item.PaymentCode);
if (payInfo != null)
{
payList.Add(new OrderPaymentPrintOutputDto()
{
PaymentName = payInfo.PaymentName,
PayAmount = item.PayAmount.RemoveZeroNum(),
PayContent = item.PayContent
});
}
}
#region 相同支付方式的金额求和
List<OrderPaymentPrintOutputDto> result = new List<OrderPaymentPrintOutputDto>();
if (payList != null && payList.Any())
{
result = (from it in payList
group it by new { it.PaymentName } into g
select new OrderPaymentPrintOutputDto
{
PaymentName = g.Key.PaymentName,
PayAmount = g.Sum(it => it.PayAmount)
}).ToList();
}
#endregion