Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i want to check in my data table that ...
my datatable contain one colum that contain value only YES and NO.

Now i want to check that atleast one "YES" row in my datatable.
how can i do that.
Posted
Updated 28-Sep-14 20:32pm
v3
Comments
Sinisa Hajnal 29-Sep-14 2:31am    
Which database? Most of them have EXISTS keyword / function - google around for your particular database. This is NOT C# qustion...
Bernhard Hiller 29-Sep-14 8:18am    
That's a terrible design. Ever heard of the BOOLEAN datatype?

At least one row implies a row count > 0, so just Select() from your DataTable and see if you have more than 0 rows returned :
http://www.dotnetperls.com/datatable-select[^]
 
Share this answer
 
Comments
Raul Iloc 29-Sep-14 2:47am    
Is not OK to execute SELECT, because your code will consume more resource and time then a simple "Count()". See details in my solution!
Mehdi Gholam 29-Sep-14 3:38am    
The LINQ is doing the same thing under the covers.
Raul Iloc 29-Sep-14 6:25am    
Is not the same. The LINQ will generate SELECT COUNT(*) FROM ... WHERE that is different then: SELECT * FROM ... WHERE, get the data in DataTable, and only after that to count the results, because even the execution of the SELECT * take longer then SELECT COUNT(*), and to fill all results could take a lot of time, depend on the amount of data from the table.
Mehdi Gholam 29-Sep-14 7:34am    
Generally that would be true, but DataTable does not support the full "sql query" commands.
Raul Iloc 30-Sep-14 7:18am    
Maybe is a misunderstanding regarding the original question. The question is about the data from a data table, and this means in my understanding the data from a database table, and your understood "DataTable" object with data inside it.
You should use Count() over an LINQ expression like in the next example:
C#
//Declare the LINQ expression 
var query = from wa in dataContext.YourTable
        where wa.YourYesNoColumn == true; 
        select wa;
//
int count = query.Count(); //Execute the Count over the LINQ expression.
return count > 0; //Return true if exist at least one result!
 
Share this answer
 
DataRow[] foundRows = dt.Select("page_name='Management'");
 
Share this answer
 
v3
you can use linq query to get count that contains "YES" value for particular column name


int count = dtCustomer.AsEnumerable().Count(x => x.Field<string>("ColumnName") == "YES" );
 
Share this answer
 

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