Merge list with Linq.

这么个问题,有两个对象如下,需要合并为一个对象。他们的是同一个证券(Security)的不同分红(Dividend).但是因为type不一样,是先做的取在合并,不是在数据库层合并的。

<DividendHistory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.xignite.com/services/">
<Outcome>Success</Outcome>
<Identity>IP</Identity>
<Delay>0.109375</Delay>
<Security>
<Outcome>Success</Outcome>
<Delay>0</Delay>
<CIK>0001053092</CIK>
<Cusip>22542D449</Cusip>
<Symbol>SLVO</Symbol>
<ISIN>US22542D4491</ISIN>
<Valoren>21187144</Valoren>
<Name>
CS Nassau Exchange Traded Notes 2013-21.4.33 Sr Lkd to CS NASDAQ Silver FLOWS 106 Idx
</Name>
<Market>NASDAQ</Market>
<CategoryOrIndustry>MoneyCenterBanks</CategoryOrIndustry>
</Security>
<Dividends>
<Dividend>
<Outcome>Success</Outcome>
<Delay>0</Delay>
<Currency>USD</Currency>
<Code>CashPayment</Code>
<Type>OrdinaryDividend</Type>
<PaymentFrequency>Monthly</PaymentFrequency>
<DeclaredDate/>
<RecordDate>11/20/2014</RecordDate>
<PayDate>11/25/2014</PayDate>
<ExDate/>
<DividendAmount>0.1114</DividendAmount>
<DataConfidence>Valid</DataConfidence>
</Dividend>
</Dividends>
</DividendHistory>
<DividendHistory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.xignite.com/services/">
<Outcome>Success</Outcome>
<Identity>IP</Identity>
<Delay>0.109375</Delay>
<Security>
<Outcome>Success</Outcome>
<Delay>0</Delay>
<CIK>0001053092</CIK>
<Cusip>22542D449</Cusip>
<Symbol>SLVO</Symbol>
<ISIN>US22542D4491</ISIN>
<Valoren>21187144</Valoren>
<Name>
CS Nassau Exchange Traded Notes 2013-21.4.33 Sr Lkd to CS NASDAQ Silver FLOWS 106 Idx
</Name>
<Market>NASDAQ</Market>
<CategoryOrIndustry>MoneyCenterBanks</CategoryOrIndustry>
</Security>
<Dividends>
<Dividend>
<Outcome>Success</Outcome>
<Delay>0</Delay>
<Currency>USD</Currency>
<Code>CashPayment</Code>
<Type>OrdinaryDividend</Type>
<PaymentFrequency>Monthly</PaymentFrequency>
<DeclaredDate/>
<RecordDate>12/22/2014</RecordDate>
<PayDate>12/29/2014</PayDate>
<ExDate>12/18/2014</ExDate>
<DividendAmount>0.2054</DividendAmount>
<DataConfidence>Valid</DataConfidence>
</Dividend>
</Dividends>
</DividendHistory>

传统做法肯定是定义Dictionary存储在合并,但是Linq做起来很是方便直观。

var divOfInterestPayments = new List<DividendHistory>();
ConvertAPIDividendHistoriesToUserDividendHistories(Exchange, SecurityMaster.API.DividendAPI.FindDividendsByExchangeOfType(CphId, dataProvider, Exchange, SecurityMaster.Schema.PaymentTypes.InterestPayment, StartDate, EndDate, MAX_DIVIDENDS_RETURNED, ref exceeded), divOfInterestPayments);

int leftCnt = MAX_DIVIDENDS_RETURNED - divOfInterestPayments.Count;
if (leftCnt < 0) leftCnt = 0;
var divOfCashPayments = new List<DividendHistory>();
ConvertAPIDividendHistoriesToUserDividendHistories(Exchange, SecurityMaster.API.DividendAPI.FindDividendsByExchangeOfType(CphId, dataProvider, Exchange, SecurityMaster.Schema.PaymentTypes.Cash, StartDate, EndDate, leftCnt, ref exceeded), divOfCashPayments);

result = divOfInterestPayments.Concat(divOfCashPayments)
        .GroupBy(d => d.Security.Valoren.Trim())
	.Select(g => g.Aggregate((d1, d2) => new DividendHistory
        {
             Security = d1.Security,
             Dividends = d1.Dividends.Concat(d2.Dividends).OrderBy(d => 
			{
				if (!string.IsNullOrEmpty(d.ExDate))
					return Convert.ToDateTime(d.ExDate);
				else if (!string.IsNullOrEmpty(d.PayDate))
					return Convert.ToDateTime(d.PayDate);
				else if (!string.IsNullOrEmpty(d.RecordDate))
					return Convert.ToDateTime(d.RecordDate);
				else
					return DateTime.MinValue;
			}).ToArray()
	})).ToList();	                        

  

原文地址:https://www.cnblogs.com/bwangff/p/4316070.html