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 RouseTask LeadAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Chinmay PatelChief Technical NinjaCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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 RouseTask LeadAuthor 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 RouseTask LeadAuthor Commented:
I will update this with my solution.  Still working on it.  Thanks for all the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.