Click here to Skip to main content
15,793,277 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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):

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:

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.
Updated 20-Aug-15 12:11pm
Maciej Los 20-Aug-15 17:12pm    
What is expected output?

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)
Share this answer
NJ44 21-Aug-15 5:34am    
Hi DVL.. thanks for your response. But actually I am not getting bs.Classification in the innermost select. None of the properties of BalanceSummary are comming if m doing bs.__ , instead Linq operations like Select, Remove are coming.

Basically WebApiResponse.BalanceSummary is a collection and should be assigned to VoiceZone.BalanceSummary collection, only when the name of the PricePlan, i.e. PricePlanName = "International OnePrice"
D V L 21-Aug-15 9:01am    
solution updated check it
NJ44 31-Aug-15 12:50pm    
Thank you DVL for the answer. It gives the required output now. Sorry for delay in response.
D V L 1-Sep-15 8:33am    
Any time :)
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...
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900