Implement a Properties Filter in .NET
I have to filter a list of objects.
The filter should be composed by users using logical OR / AND operators, and grouping using bra开发者_JAVA技巧ckets.
say, something like this:
Say, we have object MyObj and its properties Prop1, Prop2, Prop3
Having myObjList the user could filter elements that Prop1 == aValue AND Prop2 < otherValue OR Prop2 > thirdvalue
Is there some known (reflection) mechanisms that permits to manage this kind of filtering operations?
You can use dynamic linq to construct filter statements at runtime agaist object list.
Here is a link to Scott Guthrie's blog that explains implementation.
Build expression trees, then compile them to give you a delegate which you can apply to the objects you want to filter.
MSDN has some introductory coverage: http://msdn.microsoft.com/en-us/library/bb397951.aspx
Here is another link to implement dynamic linq, using more of a specification pattern.
精彩评论