Click here to Skip to main content
13,355,018 members (58,999 online)
Rate this:
Please Sign up or sign in to vote.
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.

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 20-Apr-12 22:09pm
VJ Reddy 9-May-12 4:35am
Thank you for accepting the solution.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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);
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 .
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
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
var filteredTable = (from n in datatable.AsEnumerable()
                    where !n.Field<string>("stauscode").Contains("JOB")
                    select n).CopyToDataTable();
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
VJ Reddy 24-Apr-12 3:04am
Thank you, Abhinav.
member60 10-May-12 2:24am
My 5!
VJ Reddy 10-May-12 3:15am
Thank you, member60.
Sandeep Mewara 10-May-12 14:22pm
My 5!
VJ Reddy 10-May-12 20:16pm
Thank you, Sandeep.
Juhi Paunikar 4-Apr-14 6:59am
Its also helped me

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180111.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100