Solved

2 Foreach loops

Posted on 2011-09-20
3
143 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 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