Solved

Refactoring two foreach loop into LINQ

Posted on 2014-09-15
6
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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