tags:

views:

45

answers:

2

In SQL one could pass a complex where clause as a string like some date calculation, this even at runtime. For example, letting a (poweruser) user define the where clause which can contains custom predefined functions like date functions.

How to do the same thing in pure C# when filtering a datatable ?

+1  A: 

The easiest way is to use Linq to query your DataTable.

If you don't want or can't use Linq then you can use a DataView with a RowFilter. You can type things like:

dataView.RowFilter="LastName = 'Smith'";

or:

dataView.RowFilter="Isnull(Col1,'Null Column') = 'Null Column'"

The DataView documentation includes examples of how to connect it to a DataTable.

The expression syntax is here. There are a lot of things defined, but it seems that there is not much for DateTimes unfortunately.

Mark Byers
Hi the problem is the functions included in expression syntax is limited to what Microsoft allowed. I want to use CUSTOM functions.But thanks for answer.
programmernovice
+1  A: 

If you are using LINQ you can use this dynamic implementation that creates expression trees out of LINQ-like strings.

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Josh Einstein
Hi it seems that what I need"In a future blog post I'll delve further into building dynamic LINQ queries"but it seems there's no part II yet :)
programmernovice