zoukankan      html  css  js  c++  java
  • 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();	                        
    

      

  • 相关阅读:
    004-核心技术-netty概述、传统IO、Reactor线程模型
    003-核心技术-IO模型-NIO-基于NIO群聊示例
    002-核心技术-IO模型-NIO【Selector、Channel、Buffer】、零拷贝
    018-redis-命令合计
    【整理】js、python、java分别对url进行编码和解码
    深度 | 翟东升:写在美帝国撤军和阿富汗政权溃散之际
    修改Windows10 命令终端cmd的编码为UTF-8 && IDEA控制台输出中文乱码
    Dockerfile文件中的ENTRYPOINT,CMD命令跟k8s中command,args之间的关系
    服务器带宽,流量之间的关系
    值得收藏的下载地址
  • 原文地址:https://www.cnblogs.com/bwangff/p/4316070.html
Copyright © 2011-2022 走看看