将表达式传递给NHibernate中的方法会导致“ConstantExpression”类型的对象无法转换为“LambdaExpression”类型 [英] Passing an expression to a method in NHibernate results in Object of type 'ConstantExpression' cannot be converted to type 'LambdaExpression'

查看:151
本文介绍了将表达式传递给NHibernate中的方法会导致“ConstantExpression”类型的对象无法转换为“LambdaExpression”类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题发生在NHibernate 2和3中。我有一个具有B类成员集的类A.直接执行类查询直接执行。但是当我将一个涉及B类的表达式传递给一个方法时,我会收到以下错误:

This problem occurs in both NHibernate 2 and 3. I have a Class A that has a member set of class B. Querying the classes directly executes nicely. But when I pass one of the expressions involving class B into a method I get the following error:

System.ArgumentException:类型为System.Linq.Expressions.ConstantExpression的对象'无法转换为类型'System.Linq.Expressions.LambdaExpression'。

System.ArgumentException: Object of type 'System.Linq.Expressions.ConstantExpression' cannot be converted to type 'System.Linq.Expressions.LambdaExpression'.

据我所知,我将完全相同的表达式传递给Any()方法。但是由于某种原因,它们被视为不同。我做了一些调试,看起来像第一个方法,表达式被视为一个表达式与NodeType'Quote',而第二个方法中的相同表达式似乎被视为一个表达式与NodeType'Constant'。第二种方法中表达式的父表达式具有NodeTypeMemberAccess。所以在不同的测试方法中,表情树看起来就不一样了。我只是不明白为什么以及如何解决这个问题。

As far as I can see I am passing the exact same expression into the Any() method. But for some reason they are treated differently. I have done some debugging and it looks like in the first method, the expression is treated as an expression with NodeType 'Quote', while the same expression in the 2nd method seems to be treated as an expression with NodeType 'Constant'. The parent expression of the expression in the 2nd method has a NodeType 'MemberAccess'. So it looks like the expression tree is different in the different test methods. I just don't understand why and what to do to fix this.

Classes involvend:

Classes involvend:

public class A
{
   public virtual int Id { get; set; }
   public virtual ISet<B> DataFields { get; set; }
}

public class B
{
   public virtual int Id { get; set; }
}

样本测试代码:

    [TestMethod]
    public void TestMethod1()
    {
        using (ISession session = sessionFactory.OpenSession())
        {
            var records = session.Query<A>()
                                 .Where<A>(a => a.DataFields
                                                 .Any(b => b.Id == 1));
            Console.Write("Number of records is {0}", records.Count());
        }
    }

    [TestMethod]
    public void TestMethod2()
    {
        GetAsWhereB(b => b.Id == 1);
    }

    private void GetAsWhereB(Func<B, bool> where) 
    {
        using (ISession session = sessionFactory.OpenSession())
        {
            var records = session.Query<A>()
                                 .Where(a => a.DataFields
                                              .Any(where));
            Console.Write("Number of records is {0}", records.Count());
        }
    }


推荐答案

相当肯定如果这是正确的解决方案。问题就像一个bug和我的解决方案,就像一个解决方法。然而,以下的工作适用于我,它归结为通过使用其body和参数来构造一个新的表达式来创建给定表达式的副本。

Not quite sure if this is the proper solution or not. The problem feels like a bug and my solution like a workaround. Nonetheless the following works for me, which boils down to creating a 'copy' of the given expression by using its body and parameter to construct a new expression.

private void GetAsWhereB(Func<B, bool> where) 
{
    Expression<Func<T, bool>> w = Expression.Lambda<Func<T, bool>>(where.Body, where.Parameters);
    using (ISession session = sessionFactory.OpenSession())
    {
        var records = session.Query<A>()
                             .Where(a => a.DataFields
                                          .Any(w));
        Console.Write("Number of records is {0}", records.Count());
    }
}

这篇关于将表达式传递给NHibernate中的方法会导致“ConstantExpression”类型的对象无法转换为“LambdaExpression”类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆