LINQ(使用Lambda)在嵌套集合中的用法 [英] LINQ (with Lambda) usage in nested collections
本文介绍了LINQ(使用Lambda)在嵌套集合中的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我需要你的帮助来编写一个linq查询(带有lambda表达式)用于以下场景。
AIM:我收到来自Web API的响应,其结构如下:
响应包含4个嵌套级别(收集):
WebAPIResponse:
CycleSummary(n) - > StartDate(1),EndDate(1),ServiceSummary(n) - >
ServiceID(1),ServiceTN(1),PricePlanSummary(n) - >
PricePlanName(1),InclusiveMinutes(1),BalanceSummary(n) - >
分类(1),CallCount(1),ChargeableSeconds(1),DollarAmount(1)
CycleSummary,ServiceSummary,PricePlanSummary和BalanceSummary都是类,'n'代表一个集合'n'个元素(数组)。现在我想过滤此响应并将此响应中收到的值分配给另一个对象。该对象称为IOPCallTypeModel(在** VoiceZone **名称空间内),具有以下结构:
IOPCallTypeModel->
StartDate(1),EndDate(1),ServiceSummaries(n) - >
ServiceTN,BalanceSummary(n) - >
CallType(1),持续时间(1),费用(1),NumberOfCalls(1)
工作周围:
以下是使用for循环实现此目的的编码。但是我需要使用Linq和Lambda表达式来编写相同的内容。
var cycleSummaries = UDSClient.GetPlanCycleBalanceSummary(request,_sessionId).Items.Select(item = > JsonConvert.DeserializeObject< GetPlanCycleBalanceSummary.CycleSummary>(item.ToString()) ); // 调用WebAPI并获得响应
int csCount = 0 ; int ssCount = 0 ; int ppsCount = 0 ; int bsCount = 0 ;
csCount = cycleSummaries.Count();
iOPBillingPeriods = new IOPCallTypeModel [csCount];
for ( int i = 0 ; i < csCount; i ++)
{
foreach (WebAPIResponse.CycleSummary cs in cycleSummaries)
{
iOPBillingPeriods [i] = new IOPCallTypeModel();
iOPBillingPeriods [i] .StartDate = cs.FromDate;
iOPBillingPeriods [i] .EndDate = cs.ToDate;
ssCount = cs.ServiceSummary.Count();
iOPBillingPeriods [i] .ServiceSummaries = new VoiceZone.ServiceSummary [ssCount];
for ( int j = 0 ; j < ssCount; j ++)
{
foreach (WebAPIResponse.ServiceSummary ss in cs.ServiceSummary)
{
iOPBillingPeriods [i] .ServiceSummaries [j] = new VoiceZone.ServiceSummary();
iOPBillingPeriods [i] .ServiceSummaries [j] .ServiceTN = ss.ServiceTN;
foreach (PricePlanSummary pps in ss.PricePlanSummary)
{
if (pps.PricePlanName == 国际OnePrice)
{
bsCount = pps.BalanceSummary.Count();
iOPBillingPeriods [i] .ServiceSummaries [j] .BalanceSummary = new VoiceZone.BalanceSummary [bsCount];
for ( int l = 0 ; l < bsCount; l ++)
{
foreach (WebAPIResponse.BalanceSummary bs in pps.BalanceSummary)
{
iOPBillingPeriods [i] .ServiceSummaries [j] .BalanceSummary [l] = new VoiceZone.BalanceSummary();
iOPBillingPeriods [i] .ServiceSummaries [j] .BalanceSummary [l] .CallType = bs.Classification;
iOPBillingPeriods [i] .ServiceSummaries [j] .BalanceSummary [l] .Charges = Convert.ToDouble(bs.DollarAmount);
iOPBillingPeriods [i] .ServiceSummaries [j] .BalanceSummary [l] .Duration = Convert.ToInt32(bs.ChargeableSeconds);
iOPBillingPeriods [i] .ServiceSummaries [j] .BalanceSummary [l] .NumberOfCalls = Convert.ToInt32(bs.CallCount);
}
}
}
}
}
}
}
}
请帮助。
解决方案
试试这个
i尝试将您给定的逻辑转换为Linq概念。
并告诉我是否忘记了某些内容。 />
如果需要,您也可以尝试.ToArray()方法而不是.ToList()。
var result = cycleSummaries.Select(cs = > new IOPCallTypeModel
{
开始Date = cs.FromDate,
EndDate = cs.ToDate,
ServiceSummaries = cs.ServiceSummaries.Select(ss = > new VoiceZone.ServiceSummary
{
ServiceTN = ss.ServiceTN,
BalanceSummary = ss.PricePlanSummary
.Where(w = > w.PricePlanName == International OnePrice )
.SelectMany(pps = > pps.BalanceSummaries)
。选择(bs = > ; new BalanceSummary
{
CallType = bs.Classification,
Charges = Convert.ToD ouble(bs.DollarAmount),
持续时间= Convert.ToInt32(bs.ChargeableSeconds),
NumberOfCalls = Convert.ToInt32(bs.CallCount)
})。ToList()
}。ToList()
})。ToList();
尝试如下
IOPCallTypeModel [] iOPBillingPeriods = cycleSummaries.Select(x = >
new IOPCallTypeModel(){
StartDate = // 从x.StartDate创建和设置VoiceZone.StartDate,
// 对其余属性执行相同操作...
})ToArray的();
Hi All,
I need your help in writing a linq query (with lambda expressions) for the following scenario.
AIM: I am receiving a response from a web API, which is in the following structure:
Response contains 4 levels of nesting(of collections):
WebAPIResponse:
CycleSummary(n) -> StartDate(1), EndDate(1), ServiceSummary(n)-->
ServiceID(1), ServiceTN(1), PricePlanSummary(n) ->
PricePlanName(1), InclusiveMinutes(1), BalanceSummary(n) -->
Classification(1), CallCount(1), ChargeableSeconds(1), DollarAmount(1)
CycleSummary, ServiceSummary, PricePlanSummary and BalanceSummary all are classes, and 'n' represents a collection of 'n' elements (array). Now I want to filter this response and assign the values received in this response to another object. This object is called 'IOPCallTypeModel'(inside **VoiceZone** namespace) and has the following structure:
IOPCallTypeModel->
StartDate(1), EndDate(1), ServiceSummaries(n) -->
ServiceTN, BalanceSummary(n) -->
CallType(1), Duration(1), Charges(1), NumberOfCalls(1)
Work Around:
Following is the coding done to achieve this using for loops. But I need to write the same using Linq and Lambda expressions.
var cycleSummaries = UDSClient.GetPlanCycleBalanceSummary(request, _sessionId).Items.Select(item => JsonConvert.DeserializeObject<GetPlanCycleBalanceSummary.CycleSummary>(item.ToString())); // Calling the WebAPI and getting the response
int csCount = 0; int ssCount = 0; int ppsCount = 0; int bsCount = 0;
csCount = cycleSummaries.Count();
iOPBillingPeriods = new IOPCallTypeModel[csCount];
for (int i = 0; i < csCount; i++)
{
foreach (WebAPIResponse.CycleSummary cs in cycleSummaries)
{
iOPBillingPeriods[i] = new IOPCallTypeModel();
iOPBillingPeriods[i].StartDate = cs.FromDate;
iOPBillingPeriods[i].EndDate = cs.ToDate;
ssCount = cs.ServiceSummary.Count();
iOPBillingPeriods[i].ServiceSummaries = new VoiceZone.ServiceSummary[ssCount];
for (int j = 0; j < ssCount; j++)
{
foreach (WebAPIResponse.ServiceSummary ss in cs.ServiceSummary)
{
iOPBillingPeriods[i].ServiceSummaries[j] = new VoiceZone.ServiceSummary();
iOPBillingPeriods[i].ServiceSummaries[j].ServiceTN = ss.ServiceTN;
foreach (PricePlanSummary pps in ss.PricePlanSummary)
{
if (pps.PricePlanName == "International OnePrice")
{
bsCount = pps.BalanceSummary.Count();
iOPBillingPeriods[i].ServiceSummaries[j].BalanceSummary = new VoiceZone.BalanceSummary[bsCount];
for (int l = 0; l < bsCount; l++)
{
foreach (WebAPIResponse.BalanceSummary bs in pps.BalanceSummary)
{
iOPBillingPeriods[i].ServiceSummaries[j].BalanceSummary[l] = new VoiceZone.BalanceSummary();
iOPBillingPeriods[i].ServiceSummaries[j].BalanceSummary[l].CallType = bs.Classification;
iOPBillingPeriods[i].ServiceSummaries[j].BalanceSummary[l].Charges = Convert.ToDouble(bs.DollarAmount);
iOPBillingPeriods[i].ServiceSummaries[j].BalanceSummary[l].Duration = Convert.ToInt32(bs.ChargeableSeconds);
iOPBillingPeriods[i].ServiceSummaries[j].BalanceSummary[l].NumberOfCalls = Convert.ToInt32(bs.CallCount);
}
}
}
}
}
}
}
}
Kindly help.
解决方案
try this
i try to convert your given logic into Linq conceptually.
and also tell me if i forgot something.
You can also try .ToArray() method instead of .ToList() if required.
var result = cycleSummaries.Select(cs => new IOPCallTypeModel { StartDate = cs.FromDate, EndDate = cs.ToDate, ServiceSummaries = cs.ServiceSummaries.Select(ss => new VoiceZone.ServiceSummary { ServiceTN = ss.ServiceTN, BalanceSummary = ss.PricePlanSummary .Where(w => w.PricePlanName == "International OnePrice") .SelectMany(pps => pps.BalanceSummaries) .Select(bs => new BalanceSummary { CallType = bs.Classification, Charges = Convert.ToDouble(bs.DollarAmount), Duration = Convert.ToInt32(bs.ChargeableSeconds), NumberOfCalls = Convert.ToInt32(bs.CallCount) }).ToList() }).ToList() }).ToList();
try as below
IOPCallTypeModel[] iOPBillingPeriods =cycleSummaries.Select(x=> new IOPCallTypeModel(){ StartDate =//create and set VoiceZone.StartDate from x.StartDate, // do the same for rest of the properties... }).ToArray();
这篇关于LINQ(使用Lambda)在嵌套集合中的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文