Documentation for QuerySpec .Any()

Posts   
 
    
Findev
User
Posts: 107
Joined: 08-Dec-2014
# Posted on: 05-Oct-2020 15:40:17   

Hi,

SQL Server, LLBLGen v.5.7.x

var qf = new QueryFactory();

var q = qf.Create()
          .Select
              (
               qf.XXX
                 .Select(XXX.Id)
                 .Where(XXX.JobId == Guid.NewGuid())
                 .Any()
              );

var exists = this.AdapterToUse.FetchScalar<bool>(q);

noticed that this query works fine even though it doesn't match any records but when the target table has data and fails when there's no data with "The database returned a NULL value however the type specified ('System.Boolean') isn't a nullable type". Can't say how .Any() was envisioned to work, but the solution is to use a nullable, i.e. .FetchScalar<bool?>(q) Docs seem to suggest that too, though there ?? is applied to a non-nullable values. Probably this needs to be corrected simple_smile

Thank you!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 05-Oct-2020 16:50:10   

We'll look into it simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 06-Oct-2020 12:20:36   

The Any query is run in the projection, of a query on the same table. This is done to get from 0 and 1 to true/false values. If the table doesn't have any rows, the projection isn't run as there's no row to project in the first place so the end result is indeed NULL. if there's at least 1 row, the first row is selected, the any clause is the projection and it will return the value of the any clause (which is a nested case / else).

It indeed needs clarification in the docs. simple_smile

(Edit) We've added clarification to the v5.6 and 5.7 docs

Frans Bouma | Lead developer LLBLGen Pro