Exception when using local variables in LINQ

7 posts, 0 answers
  1. Greg
    Greg avatar
    44 posts
    Member since:
    Jun 2012

    Posted 08 Jun 2012 Link to this post

    The following query causes an exception on execution. Example uses the NorthwindEF sample database, db is the ObjectContext
    int num = 10000;
    var q = db.Orders.Where(o => o.OrderID > num);
    var result = db.Customers.Where(c => q.Any(o => o.CustomerID == c.CustomerID)).ToList();

    I'm getting the following exception:
    An exception occured during the execution of 'Extent<Customers>().Where(c => value(<>c__DisplayClass2).q.Any(o => (o.CustomerID == c.CustomerID)))'.
    Failure: Query parameter name is not declared.
    Parameter name: expression
    Actual value: value(<>c__DisplayClass2).num.

    If I use the actual constant value instead of the num variable, the query works, however in my scenario I'm unable to make any assumption about how q came to be except that it's an IQueryable<T> created over the same object context (db).
  2. Greg
    Greg avatar
    44 posts
    Member since:
    Jun 2012

    Posted 12 Jun 2012 Link to this post

    Issue still present in Q2 release (2012.2.607.1)
  3. DevCraft banner
  4. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 25 Jun 2012 Link to this post

    Hi Greg,

    I must confirm that this is an issue in our detection of parameters. If you use the query directly instead of the intermediate variable q, the whole expression works.
    We will be working on this. Thanks for reporting this issue; I've updated your Telerik Points.

    All the best,
    Thomas
    the Telerik team
    OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
  5. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 25 Jun 2012 Link to this post

    Hello Greg,

    the issue is fixed now and the next version will contain the needed bits.

    Thanks again for reporting this issue.
    All the best,
    Thomas
    the Telerik team
    OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
  6. Greg
    Greg avatar
    44 posts
    Member since:
    Jun 2012

    Posted 08 Nov 2013 Link to this post

    Sorry, this is still not fixed as of 2013.3.1014.1

    partial class EntitiesModel1
    {
        public IQueryable<Order> OrdersFiltered
        {
            get
            {
                var num = 10000;
                return this.Orders.Where(o => o.OrderID > num);
            }
        }
        public IQueryable<Customer> CustomersFiltered
        {
            get { return this.Customers.Where(c => this.OrdersFiltered.Any(o => o.CustomerID == c.CustomerID)); }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            using( var db = new EntitiesModel1() )
            {
                var result = db.CustomersFiltered.ToList();
            }
        }
    }




  7. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 11 Nov 2013 Link to this post

    Hello Greg,

    we are sorry! If you could use a constant for the time being, then the query would work immediately. However, the fix is underway and will be available with the next build, scheduled for the end of the month. I just added it along with the tests.

    Thanks for reporting this issue again.
    Regards,
    Thomas
    Telerik
    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
  8. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 03 Mar 2014 Link to this post

    Hi Greg,

    Thank you for your patience and excuse us for the inconvenience caused.

    The needed fix is available with version Q3 2013 SP2 or higher of Telerik Data Access. At the time present, both versions of the query you posted can be executed exactly as they are. The SQL statement generated for them is:
    SELECT *
    FROM [Customers] a
    WHERE EXISTS (SELECT b.[OrderID]
              FROM [Orders] b
              WHERE b.[OrderID] > ? AND b.[CustomerID] = a.[CustomerID])
    ORDER BY a.[CustomerID]

    I hope you find this feasible. In case you have additional questions, or experience issues, do not hesitate to contact us.



    Regards,
    Doroteya
    Telerik
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
Back to Top
DevCraft banner