Solved

Refactoring two foreach loop into LINQ

Posted on 2014-09-15
6
220 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now