user1720794
user1720794

Reputation: 11

pass list values in where condition in linq for filtering the data in datatble

I have a requirement that i am having one string[] array (which is having the id values which is , separated values) and dt as datatable.

dt is having the columns called Id, empname, designation.

now i want to filter the data from the datatable where id not in (string[] values) using linq query.

for ex:

string[] ids= [2,4,6];

dt=  id      empname     designation
    ----     -------     ------------
     1       robert       trainer
     2       thomas       HRA
     3       John         JE
     4       kapil        SE
     5       sachin       SSE
     6       Rahul        Manager

now i want a linq query which return my dt as:

 id      empname     designation
----     -------     ------------
 1       robert       trainer 
 3       John         JE
 5       sachin       SSE

Upvotes: 1

Views: 1069

Answers (1)

cuongle
cuongle

Reputation: 75306

You can use LINQ To DataTable:

var result = dt.AsEnumerable()
               .Where(row => !ids.Contains(row.Field<string>("Id"));

Upvotes: 1

Related Questions