Why do I get "variable referenced from scope but not defined" error when using ServiceStack OrmLite?
Asked Answered
I

0

6

I have an SQL Server table Employee with a column EntryDate defined as DATETIME.

I also have the following poco:

public class Employee
{
    public int Id {get; set;}
    public DateTime EntryDate {get; set;}
    ...
}

When I query the table using:

Db.Select<Employee>(e => e.EntryDate >= new DateTime(2014, 8, 15));

Or:

Db.Select<Employee>(q => q.Where(e => e.EntryDate >= new DateTime(2014, 8, 15)));

I get what I expect, however when I try to run:

Db.Select<Employee>(e => e.EntryDate.Date >= new DateTime(2014, 8, 15).Date));

Or:

Db.Select<Employee>(q => q.Where(e => e.EntryDate.Date >= new DateTime(2014, 8, 15).Date));

I get:

variable 'e' of type 'Employee' referenced from scope '', but it is not defined

Just to confirm, writing raw SQL also works fine.

Any ideas?

Iong answered 18/8, 2014 at 15:50 Comment(5)
Something in my brain is ringing the == vs .equals() argument here, but I could be incorrect about that. And not literally, like the definition behind what x == y and x.equals(y) means.Argument
@WhyCry, it's not the .equals() as that's the first thing I tried which gives me the same errorIong
My guess is that Date is a property on the DateTime structure, but it doesn't exist in SQL which the ORM translates the expression to.Sartor
OrmLite's expression parser doesn't support transforming nested property access like this into server-side SQL.Cognate
I'm running into this as well. @Cognate OrmLite should really give a better error message in this case. A user can waste a lot of time on this.Landman

© 2022 - 2024 — McMap. All rights reserved.