Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have datatable in that one column i have data like this
emp
def
abc
key
board
cell
phone
asdf
ghij
student
klmn
opq
rst
uvw
xyz
project

like this data i have.

now i have 3 srtings like
s1=emp
s2=student
s3=project

this 3 constant strings is their or not in my datatable i want to check.

i am using for loop first its checking s1 =emp later its going to take aother one

i want to check this 3 is their or not in datatable please tell me how to do.


C#
string s1 ="emp";
for (int i = 0; i <dt.rows.count;i>
     {
             s1 = dt.Rows[i][2].ToString(); //here 2 is column index
         }
Posted
Updated 17-May-12 3:19am
v6
Comments
Zoltán Zörgő 17-May-12 9:04am    
Where is the database? I see only an array. You need T-SQL or C# code?
Wendelius 17-May-12 9:05am    
Is all that data really on the same data row or are they on separate rows?
MAKReddy 17-May-12 9:08am    
hi milka separate rows but particular column data
MAKReddy 17-May-12 9:09am    
i want code on c# code behind side how to compare that 3 strings.
in datatable that data i am getting.
MAKReddy 17-May-12 9:10am    
Hi Zoltan i am using sql server.i want to compare on datatable in codebehind

If you want to use a loop, then could something like this work for you:
C#
string s1 ="emp";
int found = 0;
for (int i = 0; i <dt.rows.count;i>
     {
             if (s1 == dt.Rows[i][2].ToString()
             || s2 == dt.Rows[i][2].ToString()
             || s3 == dt.Rows[i][2].ToString()) {
                found++;
             }
         }
if (found == 3) {
...


Of course, the actual test should check all the three variables
 
Share this answer
 
v2
Comments
MAKReddy 17-May-12 9:19am    
how it will check all 3 variables at a time. because i have 3 separate strings.
Wendelius 17-May-12 9:21am    
You can for example calculate the number of occurences. See the updated answer
VJ Reddy 17-May-12 10:58am    
Good answer. 5!
Wendelius 17-May-12 14:09pm    
Thanks
Maciej Los 17-May-12 10:58am    
Good answer, my 5!
I think the Select method of DataTable with IN clause in the filter expression can be used to return Rows of which the required column value matches any of the strings given in the IN clause as shown below:
C#
//DataTable names  with values as given in the question
//in the DataColumn Name 

string s1="emp";
string s2="student";
string s3="project";

DataRow[] foundRows = names.Select(string.Format(
                        "Name IN ('{0}','{1}','{2}')",
                        s1,s2,s3),string.Empty, 
                        DataViewRowState.CurrentRows);
Console.WriteLine (foundRows.Length);
foreach(DataRow row in foundRows)
    Console.WriteLine (row["Name"].ToString());

//Output
//  3
//emp
//project
//student


Alternatively LINQ can be used as follows

C#
string s1="emp";
string s2="student";
string s3="project";
var stringsToFind = new List<string>(){s1,s2,s3};
var foundStrings = names.AsEnumerable().Where (n => stringsToFind
        .Any (stf => stf.Equals(n.Field<string>("Name"),
        StringComparison.InvariantCultureIgnoreCase)))
        .GroupBy (n => n.Field<string>("Name"))
        .Select (n => string.Format("{0,-15}-->{1,3}",
        n.Key,n.Count ()));
foreach (string name in foundStrings)
    Console.WriteLine (name);

//Output
//emp            -->  1
//student        -->  1
//project        -->  1
 
Share this answer
 
v3
Comments
Wendelius 17-May-12 10:24am    
Also a good way :)
VJ Reddy 17-May-12 10:57am    
Thank you, Mika :)
Maciej Los 17-May-12 10:58am    
Good answer, my 5!
VJ Reddy 17-May-12 11:18am    
Thank you, losmac :)
MAKReddy 18-May-12 1:06am    
Thanks VJR
In SQL:
SQL
SELECT [Column1]
FROM [TABLE1]
WHERE [Column1] IN ('Text1','Text2','Text3')
 
Share this answer
 
Comments
VJ Reddy 17-May-12 11:25am    
Succinct and to the point. 5!
Maciej Los 17-May-12 11:32am    
Thank you, VJ ;)
Wendelius 17-May-12 14:08pm    
Yep, that's also good.
Maciej Los 17-May-12 16:05pm    
Thank you, Mika ;)
After Getting record to your datatable. Just You Need to write this line in your code

C#
int count=0;//for founding var
for (int i=0;i<dt.rows.count;i++)>
{

if(dt.rows[i][0].tostring().contains("string1")||dt.rows[i][0].tostring().contains("string2")||dt.rows[i][0].tostring().contains("string3"))
{
count++;
}
}
if(count==3)
{
//write your logic
}
 
Share this answer
 
v2
Comments
MAKReddy 18-May-12 0:56am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900