Search code examples
linqservicestackormlite-servicestack

LINQ query fails with nullable variable ormlite


I'm trying to write following LINQ query using ServiceStack Ormlite.

dbConn.Select<Product>(p => p.IsActive.HasValue && p.IsActive.Value)

Here, Product is my item class and "IsActive" is Nullable Bool property in that class. When this line executes it always throws "InvalidOperationException" with the message

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

I tried different variants as following but still same exception result

dbConn.Select<Product>(p => p.IsActive.HasValue == true && p.IsActive.Value == true)
dbConn.Select<Product>(p => p.IsActive != null && p.IsActive.Value == true)

But if I just write

dbConn.Select<Product>(p => p.IsActive.HasValue)

then it works.

I'm puzzled what is the problem? Is this servicestack ormlite issue?


Solution

  • This is nature of the Linq. In order to achieve what you need, you will need to use two where closes:

     dbConn.Where<Product>(p => p.IsActive.HasValue).Where(p=>p.Value==true);