This is a migrated thread and some comments may be shown as answers.

Exception when using local variables in LINQ

6 Answers 360 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Greg
Top achievements
Rank 1
Greg asked on 08 Jun 2012, 02:15 PM
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).

6 Answers, 1 is accepted

Sort by
0
Greg
Top achievements
Rank 1
answered on 12 Jun 2012, 07:31 AM
Issue still present in Q2 release (2012.2.607.1)
0
Thomas
Telerik team
answered on 25 Jun 2012, 12:13 PM
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.
0
Thomas
Telerik team
answered on 25 Jun 2012, 02:38 PM
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.
0
Greg
Top achievements
Rank 1
answered on 08 Nov 2013, 10:25 AM

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




0
Thomas
Telerik team
answered on 11 Nov 2013, 09:40 PM
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!
0
Doroteya
Telerik team
answered on 03 Mar 2014, 06:56 AM
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.
Tags
LINQ (LINQ specific questions)
Asked by
Greg
Top achievements
Rank 1
Answers by
Greg
Top achievements
Rank 1
Thomas
Telerik team
Doroteya
Telerik team
Share this question
or