开发者

Complex (custom method call) Dynamic Filtering at runtime in C#?

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 ?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜