How to write a ForEach statement to find an Integer based on a string value

I need to know how to write a ForEach Loop to find a string value that correlates to an integer. Is this possible?

ForEach ID in StringField
  Do Something
Crystal RouseWeb Developer/Database AdminAsked:
Who is Participating?
 
Chinmay PatelEnterprise ArchitectCommented:
Hi Crystal,

As you have tagged Linq I am presenting another snippet which is more or less same as mentioned above.
private void Process()
        {
            string values = "1,2,3,4";
            values.Split(',').ToList().ForEach(x => { int n; int.TryParse(x, out n); DoIt(n); });
        }

        private void DoIt(int n)
        {
// Here you will process the number as per your requirements

        }

Open in new window


Regards,
Chinmay.
0
 
p_davisCommented:
is it a list of strings or a delimited string?
foreach(string str in listofstrings)
{
      int id = Convert.ToInt32(str);
}

i would actually recommend tryparse for the conversion. but this is quick
0
 
p_davisCommented:
if something like comma delimted...

foreach(string str in delimitedstring.Split(',').ToList())
{
      int id = Convert.ToInt32(str);
}
1
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Fernando SotoRetiredCommented:
Some more information would be helpful such as the type of object of StringField. Is it a List an Array a simple string? If a delimiter what is it?
0
 
Crystal RouseWeb Developer/Database AdminAuthor Commented:
We have a form where a user will select from a dropdown the string value.  I'll probably use a button to submit the form.  I need to get that string value and loop thru records in a table to find all the rows that contain the string value in the field.  I need to update the database for the returned ID's.
0
 
sarabandeCommented:
you may think of using DropDownList.SelectedIndex property to get the zero-based index of the string that was selected.

you don't have to convert the string to integer if the database id is a string value as well.

Sara
0
 
Crystal RouseWeb Developer/Database AdminAuthor Commented:
I will update this with my solution.  Still working on it.  Thanks for all the help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.