Uploaded image for project: 'NHibernate [Moved to GitHub]'
  1. NH-2511

System.ArgumentException: Object of type 'System.Linq.Expressions.ConstantExpression' cannot be converted to type 'System.Linq.Expressions.LambdaExpression' when passing an expression through a method

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Trivial
    • Resolution: Fixed
    • Affects Version/s: 3.0.0.GA
    • Fix Version/s: 3.3.0.CR1
    • Component/s: Linq Provider
    • Labels:
      None
    • Sprint:

      Description

      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: Object of type 'System.Linq.Expressions.ConstantExpression' cannot be converted to type 'System.Linq.Expressions.LambdaExpression'.

      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:

      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; }

      }
      Sample test code:

      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)).AsEnumerable();
      Console.Write("Number of records is {0}

      ", records.Count());
      }
      }

        Attachments

          Issue links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                corpsusvitale corpsus vitale
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Who's Looking?