Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

2 Foreach loops

Posted on 2011-09-20
3
Medium Priority
?
149 Views
Last Modified: 2012-05-12
I have 2 rows of data like below and my code. I get the rows from database, I check for comma, and add to listbox. But what I'm doing below actually
add 5 rows and I think it's because of the 2 for each loops nested. I need 3 rows...not 5. Two of the rows get repeated...


     Bob Jones, Dave Jones      1, 11
        Jane Doe            12


I should get:
Bob Jones
Dave Jones
Jane Doe

But I get:

Bob Jones
Dave Jones
Bob Jones
Dave Jones
Jane Doe
foreach (var p in results)
                {
                    if (p.HCproviders.Contains(","))
                    {
                        String[] providerValues = p.HCproviders.Split(new Char[] { ',' });
                        String[] idValues = p.hcids.ToString().Split(new Char[] { ',' });
                        foreach (String i in providerValues)
                        {
                            foreach(string j in idValues)
 
                              lb.Items.Add(new ListItem(i, j));
                        } 
 

                    }
                    else
                    lb.Items.Add(new ListItem(p.HCproviders, p.hcids.ToString()));

                }

Open in new window

0
Comment
Question by:Camillia
[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 Comments
 
LVL 16

Accepted Solution

by:
disrupt earned 2000 total points
ID: 36567670
source:
http://forum.logicalgamers.com/source-codes/31626-c-remove-duplicates-listbox.html
public void RemoveListboxDuplicates(ListBox ListBox) 
  { 
    List<String> Removed = new List<String>(); 
    for (int I = 0; I < ListBox.Items.Count;I++) 
    { 
    if (!Removed.Contains(ListBox.Items[I].ToString())) 
    { 
    Removed.Add(ListBox.Items[I].ToString()); 
    } 
    } 
    ListBox.Items.Clear(); 
    foreach (String S in Removed) 
    { 
    ListBox.Items.Add(S); 
    } 

  }

Open in new window

0
 
LVL 5

Expert Comment

by:zvytas
ID: 36567677
Use the following code instead:

foreach (var p in results)
                {
                    if (p.HCproviders.Contains(","))
                    {
                        String[] providerValues = p.HCproviders.Split(new Char[] { ',' });
                        String[] idValues = p.hcids.ToString().Split(new Char[] { ',' });
                        int index = 0;
                        foreach (String i in providerValues)
                        {
                            lb.Items.Add(new ListItem(i, idValues[j]));
                        }
 

                    }
                    else
                    lb.Items.Add(new ListItem(p.HCproviders, p.hcids.ToString()));

                }
0
 
LVL 7

Author Comment

by:Camillia
ID: 36575885
You have
 lb.Items.Add(new ListItem(i, idValues[j]));

What is j? did you mean "index"??
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

704 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