如何构建() => x.prop lambda 表达式动态?

发布于 2024-10-14 13:22:41 字数 451 浏览 1 评论 0原文

我有这样的代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我乃一代侩神 2024-10-21 13:22:41

您可以此处了解如何构造 lambda,但在您的案例:

var arg = Expression.Constant(null, typeof(DepartmentPaperConsumption));
var body = Expression.Convert(Expression.PropertyOrField(arg, propertyName),
    typeof(object));
var lambda = Expression.Lambda<Func<object>>(body);

棘手的事情是调用 OrderByAlias - 使用 MakeGenericMethod 可能是一种方法,如上面的链接所示。

You can see how to construct the lambda here, but it really is pretty simple in your case:

var arg = Expression.Constant(null, typeof(DepartmentPaperConsumption));
var body = Expression.Convert(Expression.PropertyOrField(arg, propertyName),
    typeof(object));
var lambda = Expression.Lambda<Func<object>>(body);

The tricky thing is invoking the OrderByAlias - using MakeGenericMethod may be the way, as shown in the link above.

腻橙味 2024-10-21 13:22:41

我们将使用您编写的动态 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

紫竹語嫣☆ 2024-10-21 13:22:41

我自己设法找到了一种方法,但它看起来更像是一种解决方法,马克的版本要简单得多。我一测试就会接受马克的回答。这是我的解决方法:

public class MemberModifier : ExpressionVisitor
{
    public Expression Modify(Expression expression)
    {
        return Visit(expression);
    }

    protected override Expression VisitMember(MemberExpression node)
    {
        var t = typeof (DepartmentPaperConsumption);
        var memberInfo = t.GetMember("TotalPages")[0];
        return Expression.MakeMemberAccess(node.Expression, memberInfo);
    }
}

然后在代码中

        Expression<Func<object>> exp = () => dto.TotalColorPrints;
        var mod = new MemberModifier();
        var modEx = mod.Modify(exp);
        result.OrderByAlias((Expression<Func<object>>)modEx)

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 :

public class MemberModifier : ExpressionVisitor
{
    public Expression Modify(Expression expression)
    {
        return Visit(expression);
    }

    protected override Expression VisitMember(MemberExpression node)
    {
        var t = typeof (DepartmentPaperConsumption);
        var memberInfo = t.GetMember("TotalPages")[0];
        return Expression.MakeMemberAccess(node.Expression, memberInfo);
    }
}

and then in code

        Expression<Func<object>> exp = () => dto.TotalColorPrints;
        var mod = new MemberModifier();
        var modEx = mod.Modify(exp);
        result.OrderByAlias((Expression<Func<object>>)modEx)

MemberModifier is only raw prototype, it should be more generic and not depend on DepartmentConsumption and without hardcoded "TotalPages"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文