如何构建() => x.prop lambda 表达式动态?
我有这样的代码
DepartmentPaperConsumption dto = null;
,后来我有 NHibernate QueryOver 结果,我想订购它
result.OrderByAlias(() => dto.TotalColorCopys);
,但我希望能够使用字符串动态指定 dto 的任何属性。 我尝试使用 动态 LINQ 但似乎我无法得到它。 我还尝试从头开始构建 LambdaExpression - 也没有运气。 我将不胜感激任何帮助。
I have code like
DepartmentPaperConsumption dto = null;
then later i have NHibernate QueryOver result, and i want to order it
result.OrderByAlias(() => dto.TotalColorCopys);
but I want to be able to specify any property of dto
dynamicly with string.
I tryed using Dynamic LINQ but is seems that I just can't get it.
I also tried building LambdaExpression from ground up - also without luck.
I would appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以此处了解如何构造 lambda,但在您的案例:
棘手的事情是调用
OrderByAlias
- 使用MakeGenericMethod
可能是一种方法,如上面的链接所示。You can see how to construct the lambda here, but it really is pretty simple in your case:
The tricky thing is invoking the
OrderByAlias
- usingMakeGenericMethod
may be the way, as shown in the link above.我们将使用您编写的动态 linq,或者使用表达式树
http://msdn.microsoft.com/en-us/library/bb397951.aspx
我认为没有其他解决方案
well use dynamic linq as you wrote, or use expression tree
http://msdn.microsoft.com/en-us/library/bb397951.aspx
i don't think that there are other solutions
我自己设法找到了一种方法,但它看起来更像是一种解决方法,马克的版本要简单得多。我一测试就会接受马克的回答。这是我的解决方法:
然后在代码中
MemberModifier 只是原始原型,它应该更通用,不依赖于 DepartmentConclusion 并且没有硬编码的“TotalPages”
I managed to find one way myself, but it looks more of workaround, Marc's version is way more simpler. I will accept Marc's answer as soon as i will test it. Heres my workaround :
and then in code
MemberModifier is only raw prototype, it should be more generic and not depend on DepartmentConsumption and without hardcoded "TotalPages"