对象的组和总和 [英] Group and sum list of objects
问题描述
嗨朋友们,
我有一个XML,我需要组和总和 YearlyAmts-> netAmount
基于帐户 - >类型
。我将xml反序列化并将其作为对象。现在我需要对该对象应用查询。我能够对年度金额进行分组和汇总,但我的要求是在分组时需要考虑账户类型。
预期产量
类型年份NetAmount
401k 2016 5000
VA 2016 40000
以下是示例xml
< YearlyContributions >
< Item1 >
< 贡献 >
< ContributionId > 4 < / ContributionId >
< CType > profitharing < / CType >
< CSubType > 金额< / CSubType >
< TaxTreatment > PreTax < / TaxTreatment >
< 帐户 >
< AccountID > 5 < / AccountID >
< 类型 > 401K < /类型 >
< LimitGrp > 401K < / LimitGrp >
< AcClient >
< birthyear > 1971 &l t; / birthyear >
< ClientID > 2 < / ClientID >
< endOfAnalysisYear > 2066 < / endOfAnalysisYear >
< Isprimary > N < / Isprimary >
< pensionYear > 2040 < / retirementYear >
< / AcClient >
< MarketValue > 30000 < / MarketValue >
< VAAccountType / >
< / account >
< 收入 >
< IncomeID > 2 < / IncomeID >
< 类型 > SB < / Type >
< 客户 >
< birthyear > 1971 < / birthyear >
< ClientID > 2 < / ClientID >
< endOfAnalysisYear > 2066 < / endOfAnalysisYear >
< Isprimary > N < / Isprimary >
< pensionYear > 2040 < / retirementYear >
< /客户 >
< 所有权类型 > 单个< / OwnershipType >
< TaxExempt > N < / TaxExempt >
< CertainGauranteePeriod > 0 < / CertainGauranteePeriod >
< SurvivorshipPercent > 0 < / SurvivorshipPercent >
< exclusionratio > 0 < / exclusionratio >
< ExclusionRatioExpectedDeathAge > 0 < / ExclusionRatioExpectedDeathAge >
< AmountPeriodID > 4 < / AmountPeriodID >
< / income >
< AmountPeriodID > 0 < / AmountPeriodID >
< / contribution >
< stateofmortality / < span class =code-keyword>>
< YearlyAmts >
< Item1 >
< year > 2016 < / year >
< < span class =code-leadattribute> netAmount > 50000 < / netAmount >
< < span class =code-leadattribute> / Item1 >
< Item2 >
< 年 > 2017 < / year >
< netAmount > 51000 < / netAmount >
< / Item2 >
< Item3 >
< year > 2018 < / year >
< netAmount > 52020 < / netAmount >
< / Item3 >
< / YearlyAmts >
< / Item1 < span class =code-keyword>>
< Item2 >
< 贡献 >
< ContributionId > 5 < / ContributionId >
< CType > profitharing < / CType >
< CSubType > 百分比< / CSubType >
< TaxTreatment > PreTax < / TaxTreatment >
< 帐户 >
< AccountID > 9 < / AccountID >
< 类型 > VA < span class =code-keyword>< / Type >
< LimitGrp > 401K < span class =code-keyword>< / LimitGrp >
< AcClient >
< birthyear > 1970 < / birthyear >
< ClientID < span class =code-keyword>> 1 < / ClientID >
< endOfAnalysisYear < span class =code-keyword>> 2065 < / endOfAnalysisYear >
< Isprimary < span class =code-keyword>> Y < / Isprimary >
< pensionYear < span class =code-keyword>> 2039 < / retirementYear >
< / AcClient >
< MarketValue > 30000 < / MarketValue >
< VAAccountType > 401K < / VAAccountType >
< / Account >
< 收入 < span class =code-keyword>>
< IncomeID > 1 < / IncomeID < span class =code-keyword>>
< 类型 > SB < / Type < span class =code-keyword>>
< 客户 >
< birthyear > 1970 < / birthyear >
< ClientID > 1 < / ClientID >
< endOfAnalysisYear > 2065 < / endOfAnalysisYear >
< Isprimary > Y < / Isprimary >
< retirementYear > 2039 < / retirementYear >
< / Client >
< OwnershipType > 单个< / OwnershipType >
< TaxExempt > N < / TaxExempt >
< CertainGauranteePeriod > 0 < / CertainGauranteePeriod >
& lt; SurvivorshipPercent > 0 < / SurvivorshipPercent >
< exclusionratio > 0 < / exclusionratio >
< ExclusionRatioExpectedDeathAge > 0 < / ExclusionRatioExpectedDeathAge >
< AmountPeriodID > 3 < / AmountPeriodID >
< / income >
< AmountPeriodID > 0 < / AmountPeriodID >
< /贡献 >
< stateofmortality / >
< YearlyAmts >
< Item25 >
< 年 > 2016 < / year >
< netAmount > 40000 < / netAmount >
< / Item25 >
< Item26 >
< 年 > 2017 < /年 >
< netAmount > 40800 < / netAmount >
< / Item26 >
< Item27 >
< year > 2018 < / year >
< netAmount > 41616 < / netAmount >
< / Item27 >
< ; / YearlyAmts >
< / Item2 >
< / YearlyContributions >
新增:类结构
public class YearlyContributions
{
public List< ContributionItem> Item { get ; set ; }
}
public class ContributionItem
{
public 贡献贡献{ get ; set ; }
public string stateofmortality { get 跨度>; set ; }
public YearlyAmt YearlyAmts { get ; set ; }
}
public class 贡献
{
public int ContributionId { get ; set ; }
public string CType { get 跨度>; set ; }
public string CSubType { get 跨度>; set ; }
public string TaxTreatment { get 跨度>; set ; }
public 帐户帐户{ get ; set ; }
public 收入收入{ get ; set ; }
public int AmountPeriodID { get 跨度>; set ; }
}
public class 帐户
{
public int AccountID { get ; set ; }
public string 输入{ get 跨度>; set ; }
public string LimitGrp { get 跨度>; set ; }
public 客户端AcClient { get ; set ; }
public double MarketValue { get 跨度>; set ; }
public string VAAccountType { get 跨度>; set ; }
}
public class 客户
{
public int birthyear { get ; set ; }
public int ClientID { get 跨度>; set ; }
public int endOfAnalysisYear { get 跨度>; set ; }
public string Isprimary { get 跨度>; set ; }
public int retirementYear { get 跨度>; set ; }
}
public class 收入
{
public int IncomeID { get ; set ; }
public string 输入{ get 跨度>; set ; }
public 客户端客户端{ get ; set ; }
public string OwnershipType { get 跨度>; set ; }
public string TaxExempt { get 跨度>; set ; }
public double CertainGauranteePeriod { get 跨度>; set ; }
public double SurvivorshipPercent { get 跨度>; set ; }
public double exclusionratio { get 跨度>; set ; }
public double ExclusionRatioExpectedDeathAge { get 跨度>; set ; }
public int AmountPeriodID { get 跨度>; set ; }
}
public class YearlyAmt
{
public 列表< YearlyItem> YearlyAmts { get ; set ; }
}
public class YearlyItem
{
public int year { get ; set ; }
public double NetAmount { get 跨度>; set ; }
}
我的尝试:
$ b$b
var lstcon = ycon.Item
.SelectMany(s => s.YearlyAmts.YearlyAmts)
.GroupBy(yr => yr.year) $ b$b .Select(g => new
{
Key = g.Key,
Value = g.Sum(s => s.NetAmount),
})
.ToList( );
Please, read my comment to the question first.
Second guess:
var result = items.SelectMany(i=>
i.YearlyAmt.YearlyAmts.Select(ya => new
{
ContributionId = i.Contribution.ContributionId,
AccountId = i.Contribution.Account.AccountID,
Type = i.Contribution.Account.Type,
Year = ya.year,
NetAmount = ya.NetAmount
}))
.GroupBy(x=>new{Year = x.Year, Type = x.Type})
.Select(grp=>new
{
Type = grp.Key.Type,
Year = grp.Key.Year,
NetAmount = grp.Sum(a=>a.NetAmount)
});$p $p>
In your case, above code should return:
Type Year NetAmount
401K 2016 50000
401K 2017 51000
401K 2018 52020
VA 2016 40000
VA 2017 40800
VA 2018 41616
Hi Friends,
I have an XML and i need group and sum YearlyAmts->netAmount
based upon Account -> Type
. I de- serialize the xml and make it as object. now i need to apply query over the object. I am able to group and sum yearly amount but my requirement is while grouping in need to consider Account type as well.
Expected Output
Type Year NetAmount
401k 2016 5000
VA 2016 40000
Below is the sample xml
<YearlyContributions>
<Item1>
<contribution>
<ContributionId>4</ContributionId>
<CType>profitsharing</CType>
<CSubType>Amount</CSubType>
<TaxTreatment>PreTax</TaxTreatment>
<Account>
<AccountID>5</AccountID>
<Type>401K</Type>
<LimitGrp>401K</LimitGrp>
<AcClient>
<birthyear>1971</birthyear>
<ClientID>2</ClientID>
<endOfAnalysisYear>2066</endOfAnalysisYear>
<Isprimary>N</Isprimary>
<retirementYear>2040</retirementYear>
</AcClient>
<MarketValue>30000</MarketValue>
<VAAccountType/>
</Account>
<income>
<IncomeID>2</IncomeID>
<Type>SB</Type>
<Client>
<birthyear>1971</birthyear>
<ClientID>2</ClientID>
<endOfAnalysisYear>2066</endOfAnalysisYear>
<Isprimary>N</Isprimary>
<retirementYear>2040</retirementYear>
</Client>
<OwnershipType>Single</OwnershipType>
<TaxExempt>N</TaxExempt>
<CertainGauranteePeriod>0</CertainGauranteePeriod>
<SurvivorshipPercent>0</SurvivorshipPercent>
<exclusionratio>0</exclusionratio>
<ExclusionRatioExpectedDeathAge>0</ExclusionRatioExpectedDeathAge>
<AmountPeriodID>4</AmountPeriodID>
</income>
<AmountPeriodID>0</AmountPeriodID>
</contribution>
<stateofmortality/>
<YearlyAmts>
<Item1>
<year>2016</year>
<netAmount>50000</netAmount>
</Item1>
<Item2>
<year>2017</year>
<netAmount>51000</netAmount>
</Item2>
<Item3>
<year>2018</year>
<netAmount>52020</netAmount>
</Item3>
</YearlyAmts>
</Item1>
<Item2>
<contribution>
<ContributionId>5</ContributionId>
<CType>profitsharing</CType>
<CSubType>Percent</CSubType>
<TaxTreatment>PreTax</TaxTreatment>
<Account>
<AccountID>9</AccountID>
<Type>VA</Type>
<LimitGrp>401K</LimitGrp>
<AcClient>
<birthyear>1970</birthyear>
<ClientID>1</ClientID>
<endOfAnalysisYear>2065</endOfAnalysisYear>
<Isprimary>Y</Isprimary>
<retirementYear>2039</retirementYear>
</AcClient>
<MarketValue>30000</MarketValue>
<VAAccountType>401K</VAAccountType>
</Account>
<income>
<IncomeID>1</IncomeID>
<Type>SB</Type>
<Client>
<birthyear>1970</birthyear>
<ClientID>1</ClientID>
<endOfAnalysisYear>2065</endOfAnalysisYear>
<Isprimary>Y</Isprimary>
<retirementYear>2039</retirementYear>
</Client>
<OwnershipType>Single</OwnershipType>
<TaxExempt>N</TaxExempt>
<CertainGauranteePeriod>0</CertainGauranteePeriod>
<SurvivorshipPercent>0</SurvivorshipPercent>
<exclusionratio>0</exclusionratio>
<ExclusionRatioExpectedDeathAge>0</ExclusionRatioExpectedDeathAge>
<AmountPeriodID>3</AmountPeriodID>
</income>
<AmountPeriodID>0</AmountPeriodID>
</contribution>
<stateofmortality/>
<YearlyAmts>
<Item25>
<year>2016</year>
<netAmount>40000</netAmount>
</Item25>
<Item26>
<year>2017</year>
<netAmount>40800</netAmount>
</Item26>
<Item27>
<year>2018</year>
<netAmount>41616</netAmount>
</Item27>
</YearlyAmts>
</Item2>
</YearlyContributions>
Newly Added: Class structure
public class YearlyContributions
{
public List<ContributionItem> Item { get; set; }
}
public class ContributionItem
{
public Contribution Contribution { get; set; }
public string stateofmortality { get; set; }
public YearlyAmt YearlyAmts { get; set; }
}
public class Contribution
{
public int ContributionId { get; set; }
public string CType { get; set; }
public string CSubType { get; set; }
public string TaxTreatment { get; set; }
public Account Account { get; set; }
public Income Income { get; set; }
public int AmountPeriodID { get; set; }
}
public class Account
{
public int AccountID { get; set; }
public string Type { get; set; }
public string LimitGrp { get; set; }
public Client AcClient { get; set; }
public double MarketValue { get; set; }
public string VAAccountType { get; set; }
}
public class Client
{
public int birthyear { get; set; }
public int ClientID { get; set; }
public int endOfAnalysisYear { get; set; }
public string Isprimary { get; set; }
public int retirementYear { get; set; }
}
public class Income
{
public int IncomeID { get; set; }
public string Type { get; set; }
public Client Client { get; set; }
public string OwnershipType { get; set; }
public string TaxExempt { get; set; }
public double CertainGauranteePeriod { get; set; }
public double SurvivorshipPercent { get; set; }
public double exclusionratio { get; set; }
public double ExclusionRatioExpectedDeathAge { get; set; }
public int AmountPeriodID { get; set; }
}
public class YearlyAmt
{
public List<YearlyItem> YearlyAmts { get; set; }
}
public class YearlyItem
{
public int year { get; set; }
public double NetAmount { get; set; }
}
What I have tried:
var lstcon = ycon.Item
.SelectMany(s => s.YearlyAmts.YearlyAmts)
.GroupBy(yr => yr.year)
.Select(g => new
{
Key = g.Key,
Value = g.Sum(s => s.NetAmount),
})
.ToList();
Please, read my comment to the question first.
Second guess:
var result = items.SelectMany(i=> i.YearlyAmt.YearlyAmts.Select(ya => new { ContributionId = i.Contribution.ContributionId, AccountId = i.Contribution.Account.AccountID, Type = i.Contribution.Account.Type, Year = ya.year, NetAmount = ya.NetAmount })) .GroupBy(x=>new{Year = x.Year, Type = x.Type}) .Select(grp=>new { Type = grp.Key.Type, Year = grp.Key.Year, NetAmount = grp.Sum(a=>a.NetAmount) });
In your case, above code should return:
Type Year NetAmount 401K 2016 50000 401K 2017 51000 401K 2018 52020 VA 2016 40000 VA 2017 40800 VA 2018 41616
这篇关于对象的组和总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!