Anyname Donotcare
Anyname Donotcare

Reputation: 11393

How to filter a DataTable by values not contained in an array?

If I have a DataTable userwidget, which has the following columns:

process_id, emp_num, widget_color

How to filter this DataTable using LINQ according to the following conditions:


1- WHERE emp_num = ...

2- AND process_id NOT IN (process)//process is an array of intgers

Upvotes: 2

Views: 3138

Answers (2)

Pranay Rana
Pranay Rana

Reputation: 176896

Use, where <list>.Contains( <item> )

 var lstprocessid = {1, 2, 3};
  var   rows =
        (from datatable in dtDetails.AsEnumerable()
          where !lstprocessid.Contains(int.parse((datatable["process_id "]).ToString())
              &&  int.parse((datatable["emp_num"]).ToString())== myemp_num     
                     select datatable).ToList<DataRow>();

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460068

var filtered = (from row in tbl.AsEnumerable()
               where row.Field<int>("emp_num")==yourNum
               && !process.Contains(row.Field<int>("process_id"))
               select row).CopyToDataTable();

Upvotes: 4

Related Questions