tags:

views:

64

answers:

3

I want to filter a datatable with a price range. I want to remove everything that is not between (for example) 2 and 7. In SQL I would do: NOT( price between 2 and 7). But in my filter expression I cannot use the keyword 'between'. What is the best solution?

A: 

You could try something like

dt.Select("Price < 2 OR Price > 7")

Just rember that

price between 2 and 7

is equal to

price >= 2 AND <= 7

and

NOT (price between 2 and 7)

is equal to

price < 2 OR > 7
astander
A: 

Which version of .net , you are using. You can use Linq if possible.

Var results = from t in dt.AsEnumarable()
           Where t[price]<2 || t[price]>7
           Select t
saurabh
i am using .net 4.0
Ivo
+1  A: 

If you are referencing System.Data.DataSetExtensions (which enables LINQ for DataTables), you can do the following:

var query = myDataTable.Where(c => c.Price >= 2 && c.Price <= 7);
var myDataView = query.AsDataView(); // if you want to databind...
code4life