No, it's sadly how SQL works. As the table is empty, the whole case when
clause won't run, the select will simply return null as there are no rows to execute this on. If I execute the same query on a table with rows, it will return 1 or 0. e.g.:
SELECT TOP(1)
CASE WHEN
CASE WHEN
EXISTS (
SELECT [dbo].[Orders].[orderid] AS [ClientsOwnId]
FROM [dbo].[Orders]
WHERE ( ( ( [dbo].[Orders].[orderid] = 1
AND [dbo].[Orders].[CustomerId] = 'Foo')))
)
THEN 1
ELSE 0
END=1
THEN 1
ELSE 0
END AS [LLBLV_1]
FROM
[dbo].[Orders]
will return 0
but (super is empty)
SELECT TOP(1)
CASE WHEN
CASE WHEN
EXISTS (
SELECT [dbo].Super.[id] AS [ClientsOwnId]
FROM [dbo].[Super]
WHERE ( ( ( [dbo].[Super].[id] = 1
AND [dbo].[super].[Name] = 'Foo')))
)
THEN 1
ELSE 0
END=1
THEN 1
ELSE 0
END AS [LLBLV_1]
FROM
[dbo].[super]
does not, it will return no rows.
To get around this, do this:
var qf = new QueryFactory();
var q = qf.Super.Select(SuperFields.Id, SuperFields.Name);
var result = adapter.FetchScalar<bool?>(qf.Create().Select(q.Any((SuperFields.Id == 1).And(SuperFields.Name == "Foo"))));
where you specify bool?
as the type to return (so a nullable bool)
The difference here with linq is that 'Any()' here isn't the function that's executed (in Linq we check for null and return false), here the 'Any' is part of a projection so it depends on the rows returned by that projection.