Solved

Refactoring two foreach loop into LINQ

Posted on 2014-09-15
6
233 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Different Delete Messages 7 37
Is setting this variable making a duplicate in memory 4 30
LINQ - C# to VB convertion 12 46
Replace &lt; with < 14 56
While working on Silverlight and WCF application, I faced one issue where fault exception occurred at WCF operation contract is not getting propagated to Silverlight client. So after searching net I came to know that it was behavior by default for s…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

14 Experts available now in Live!

Get 1:1 Help Now