Implement a Properties Filter in .NET

▼魔方 西西 提交于 2019-12-31 05:30:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!