3

I'm using a select method on a dataset to retreive the results that match my condition:

foreach (DataRow dr in dsPone2.Tables["tt-pone"].Select(strWhereCondition))
{
    dsPone.Tables["tt-pone"].ImportRow(dr);
}

How do I change the strWhereCondition from

strWhereCondition += " AND poneid = 0 and aoneid = 0 and tranid = 0";

To where tranid is NOT 0?

Do I use <> or !=?

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Colin
  • 1,141
  • 1
  • 9
  • 9

4 Answers4

13

As is so often the case, consulting the documentation is the way forward. DataTable.Select(string) redirects to DataColumn.Expression to document what's allowed in a filter expression - and the "Operators" list shows <> but not !=.

Personally I would try to avoid string-based filtering and use LINQ to DataSet instead, but of course that requires .NET 3.5 or higher.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    Hmm, RTFM, Good idea! To be honest, I've never got on with the C# documentation, I struggle to find what I want. Normally I can get the answer on here quicker. However, next time I have a simple question, I'll try the 'official' papers first. Thanks Jon. – Colin Mar 02 '12 at 17:43
  • @Colin: I find that a search for "msdn" along with the name of the member I'm looking for works fine. So in this case I search for "msdn datatable.select". It's *vital* that you learn to navigate the documentation effectively. – Jon Skeet Mar 02 '12 at 17:53
4

you have to use the SQL notation <>

in your case tranid <> 0

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
2

Use tranid <> 0.

The syntax for the expressions is described here.

Carsten
  • 17,991
  • 4
  • 48
  • 53
1

The documentation for the DataColumn.Expression property details the syntax used by the filterExpression parameter of the DataTable.Select method. In the Operators section about a third of the way down the page it says...

Concatenation is allowed using Boolean AND, OR, and NOT operators.

...and also lists a <> comparison operator. So, that should mean you can do either tranid <> 0 or NOT (tranid = 0).

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68