Filtering on DataTables

Filtering on DataTables

When you want to apply a filter to your datatable in your dataset, you can use the method Select on the class DataTable. In our current project we use this method very often, also for databinding.

Recently I discover an unexpected behaviour. Let’s assume, that our datatable has 4 datarows and we will apply following filter for databinding:
0=1 AND Path = ‘C:\bin’

The first expression (0=1) is added by an other method, which checks if any rows should be visible. The second expression (Path = ‘C:\bin’) is the standard part of the filter (Yes, I know you could implement it much more simplier).

Now, I programmed the following NUnit-Tests:

//0=1 -> OK
DataRow[] rowsComp = tabComp.Select(“0=1”);
Assert.IsNotNull(rowsComp);
Assert.AreEqual(0, rowsComp.Length);

//Name = ‘Component 7’ -> OK
rowsComp = tabComp.Select(“(Name = ‘Component 7’)”);
Assert.IsNotNull(rowsComp);
Assert.AreEqual(0, rowsComp.Length);

//Path -> OK
rowsComp = tabComp.Select(@”Path = ‘C:\bin'”);
Assert.IsNotNull(rowsComp);
Assert.AreEqual(4, rowsComp.Length);

//0=1 and Path (with brackets) -> OK
rowsComp = tabComp.Select(@”((0=1) AND (Path = ‘C:\bin’))”);
Assert.IsNotNull(rowsComp);
Assert.AreEqual(0, rowsComp.Length);

//Name = ‘Component 7’ and Path (without brackets) -> OK
rowsComp = tabComp.Select(@”(Name=’Component 7′) AND (Path = ‘C:\bin’)”);
Assert.IsNotNull(rowsComp);
Assert.AreEqual(0, rowsComp.Length);

//0=1 and Path (without any brackets) -> OK
rowsComp = tabComp.Select(@”0=1 AND Path = ‘C:\bin'”);
Assert.IsNotNull(rowsComp);
Assert.AreEqual(0, rowsComp.Length);

//0=1 and Path (without outer brackets) -> Unexpected behaviour
rowsComp = tabComp.Select(@”(0=1) AND (Path = ‘C:\bin’)”);
Assert.IsNotNull(rowsComp);
Assert.AreEqual(0, rowsComp.Length);

Only the last test failed. If you add around the whole expression brackets, then it works fine. Obviously the Select-Methode has a problem with expressions which doesn’t operate over a column. I didn’t try other expressions, but my current workarounds are remove all brackets or add an additional bracket around the whole expression.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.