Solved

2 Foreach loops

Posted on 2011-09-20
3
142 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
3 Comments
 
LVL 16

Accepted Solution

by:
disrupt earned 500 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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