问题
I have to filter a list of objects.
The filter should be composed by users using logical OR / AND operators, and grouping using brackets.
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?
回答1:
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.
回答2:
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
回答3:
Here is another link to implement dynamic linq, using more of a specification pattern.
来源:https://stackoverflow.com/questions/7026855/implement-a-properties-filter-in-net