Solved

Refactoring two foreach loop into LINQ

Posted on 2014-09-15
6
243 Views
Last Modified: 2016-02-15
I am having below code which needs to be converted into LINQ so basically It needs refactoring.

what can I do this for it?

List<Data> dataList = new List<Data>();  

                    foreach (var r in cntResponse.counteries)
                    {
                        Data objRecord= new Data();

                        foreach (var p in this.CntPermissions) 
                        {
                            if (p.state == "Haryana")
                            {
                                if (p.id == r.id && p.wquid == r.wquid)
                                {
                                    objRecord.Id = r.id;
                                    break;
                                }
                            }
                            else
                            {
                                if (p.id == r.id)
                                {
                                    objRecord.Id = r.id;
                                    objRecord.Organization = p.org;
                                    break;
                                }
                            }
                        }                 
                          dataList.Add(objRecord);
                    }

Open in new window


Thanks
meetDinesh
0
Comment
Question by:Dinesh Kumar
  • 3
  • 2
6 Comments
 
LVL 25

Expert Comment

by:apeter
ID: 40326884
Try below. Hope it helps.

var res = from  r in cntResponse.counteries
join p in this.CntPermissions on p.state equals "Haryana" into haryanagroup
where p.id == r.id && p.wquid == r.wquid
select new Data() { id = r.id };

if above is null then use below

var res = from  r in cntResponse.counteries
join p in this.CntPermissions on p.id equals r.id  into haryanagroup
select new Data() { id = r.id, Organization = p.org};
0
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 40327472
First let's analyse what the foreach loop does.
For each item in counteries, you have three possibilities :
- you find an item in CntPermissions with state == "Haryana" and id == r.id and p.wquid == r.wquid
- you find an item in CntPermissions with state != "Haryana" and id == r.id
- you find no item with these conditions.

var data = from r in cntResponse.counteries
           let p = this.CntPermissions.FirstOrDefault(p => p.state == "Haryana" ? p.id == r.id && p.wquid == r.wquid : p.id == r.id)
           select p == null ? new Data()
                            : p.state == "Haryana" ? new Data() { Id = r.id }
                                                   : new Data() { Id = r.id, Organization = p.org };

Open in new window

0
 

Author Comment

by:Dinesh Kumar
ID: 40335473
Hi experts, I removed inner foreach mentioned in the question, outer foreach is still to remove, let me try your suggestions will update you on this. thanks for your time.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:Dinesh Kumar
ID: 40348572
select p == null ? new Data()

Here Data() belongs to what namespace?
0
 
LVL 11

Assisted Solution

by:louisfr
louisfr earned 500 total points
ID: 40349343
I don't know. You used it in the first place.
0
 

Author Closing Comment

by:Dinesh Kumar
ID: 40396563
Thank you.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question