Solved

Refactoring two foreach loop into LINQ

Posted on 2014-09-15
6
257 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

626 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