Avatar of Prysson
Prysson

asked on 

Help finding matching values between a string[] and a DataTable

I am tryig to figure out an efficient way to handle this..

I have a DataTable that has anywhere from 2 to 6 DataRows in it depending on the results of an erlier routine. The DataRow only has one column..for the sake of argument lets say the columns name is "column1"  

I also have an array with a hard coded list of values.    

It can be assumed that one and only one of the Datarows values will NOT match one of the string values in the array. I need to find THAT datarow and dump its value into a string.

C#

Avatar of undefined
Last Comment
Prysson
Avatar of silemone
silemone
Flag of United States of America image

doesnt array use Contains method?  so if

foreach (DataRow x in MyDataTable.Rows)
{
    if(arrayName.contains(x))
{
//do something here...like get Index, etc.
}


}
Avatar of Prysson
Prysson

ASKER

to my knowledge an array does not have a Contains method..
Avatar of silemone
silemone
Flag of United States of America image

I'm sorry..it doesn't


try this then..
doesnt array use Contains method?  so if

foreach (DataRow x in MyDataTable.Rows)
{
  foreach(Object y in My Array
{
    if (x.equals (y) )
{
  //do somehting.
}

}
}


}
Avatar of silemone
silemone
Flag of United States of America image

I'm sorry..it doesn't


try this then..
doesnt array use Contains method?  so if

string str;
foreach (DataRow x in MyDataTable.Rows)
{
  foreach(Object y in My Array)
{
    if( !x.equals (y) )
      {
                  str = y.ToString();  
       }

}
}


}
Avatar of silemone
silemone
Flag of United States of America image

the way above takes array info and puts in string  if you wish to put the info from the datatable in the string,

then str = x.ToString();
ASKER CERTIFIED SOLUTION
Avatar of silemone
silemone
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Prysson
Prysson

ASKER

foreach (DataRow r in dt.Rows)
           {
               x= Convert.ToString(r["columname"]);
               bool match = false;
               for (int i = 0; i < array.Length; i++)
               {
                   string y = array[i].ToString();

                   if (y == x)
                   {
                       match = true;
                       break;
                   }                  

               }

               if (match == false)
               {
                   x is the value that doesnt match
               }
           }
Avatar of Prysson
Prysson

ASKER

Im gonna give you points for your help...

Your suggestion got me going in the right direction...please se my own final comment for the solution I used.
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo