Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi Friends,
I want to filter the rows of data table based on some condition i have tried the following code to fire with "!" and "IN" clause.

C#
var deepak = from n in datatable.AsEnumerable()
                        where !(from n1 in datatable.AsEnumerable()
                                select n1.Field<string>("stauscode").Contains("JOB"))
                        select n;


Please suggest me how to place the in and not in condition in linq on datatable
i have also tried googling but that are not very much understandable.

Thanks in Advance
Posted
Comments
VJ Reddy 9-May-12 4:35am    
Thank you for accepting the solution.

Try
C#
var list1 = from n1 in datatable.AsEnumerable() select n1.Field<string> ("stauscode").Contains("JOB")

var list2 = from n in datatable.AsEnumerable() select n

var result = list2.Except(list1);
 
Share this answer
 
v2
Comments
Dr_Sarger_PUA 27-Oct-12 2:32am    
Hi i want to modify this code , to campare two tables . the tables are the same , no primary key, i want to compare the two tables and return the rows that exists in datatable A not in datatable B .
From the question it appears that all rows from datatable, which do not contain JOB in stauscode column are required to be filtered out, as the same datatable is used in the inner query. If that is the case, then I think the following query can be used.
C#
var filteredRows = from n in datatable.AsEnumerable()
                   where !n.Field<string>("stauscode").Contains("JOB")
                   select n;

If the result is required as a DataTable then
C#
var filteredTable = (from n in datatable.AsEnumerable()
                    where !n.Field<string>("stauscode").Contains("JOB")
                    select n).CopyToDataTable();
 
Share this answer
 
v2
Comments
Abhinav S 23-Apr-12 23:18pm    
Correct. 5.
VJ Reddy 23-Apr-12 23:30pm    
Thank you, Abhinav.
I think the vote is not updated.
Abhinav S 24-Apr-12 2:56am    
Fixed.
VJ Reddy 24-Apr-12 3:04am    
Thank you, Abhinav.
member60 10-May-12 2:24am    
My 5!

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