SubQueries and new types

Posts   
 
    
rossmcw
User
Posts: 28
Joined: 27-Mar-2008
# Posted on: 19-May-2008 07:57:39   

I was trying to get the following to work (Northwind):

                var q1 =
                    from o in db.Orders
                    select new {
                        o.OrderID,
                        HasRegion = (o.ShipRegion == null) ? true : false
                    };

                var q2 = q1.Where(a => a.HasRegion == true).ToList();

but it blew up with yet another alias error.

Msg 4145, Level 15, State 1, Line 9
An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.
Msg 102, Level 15, State 1, Line 17
Incorrect syntax near 'LPA_L1'.

this was from the following sql

exec sp_executesql N'
SELECT [LPA_L1].[OrderID], [LPA_L1].[LPFA_5] 
FROM 
(
    SELECT [LPLA_1].[OrderID], CASE WHEN CASE WHEN CASE WHEN 
    (
        [LPLA_1].[ShipRegion] IS NULL
    ) 
    THEN 1 ELSE 0 END=1 THEN @LO11 ELSE @LO02 END THEN 1 ELSE 0 END AS [LPFA_5] 
    FROM [Northwind].[dbo].[Orders] [LPLA_1] 
) 
LPA_L1 WHERE 
( 
    ( 
        ( 
            ( 
                [LPA_L1].[HasRegion] = @HasRegion3
            )
        )
    )
)
',N'@LO11 bit,@LO02 bit,@HasRegion3 bit',@LO11=1,@LO02=0,@HasRegion3=1

This is interesting also because I was trying to reproduce an error I was having in another piece of work in Northwind, but I got a different error, saying the equivalent of Invalid column name 'HasRegion'. I will see if I can get this one to reproduce in Northwind too, but I havent managed yet.

Thanks! Ross

BTW: I appreciate that true and false are essentially the wrong way around but you get the idea simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39873
Joined: 17-Aug-2003
# Posted on: 19-May-2008 14:24:41   

Some unwanted CASE statements are emitted there. This can be due to the fact that the expression is in a projection: boolean expressions in the projection aren't supported in SQL. I'll check where this extra wrapper is coming from. (and also why the column isn't properly aliased to HasRegion)

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39873
Joined: 17-Aug-2003
# Posted on: 19-May-2008 18:21:34   

To be able to handle select new { o.OrderID, HasRegion = (o.ShipRegion == null) };

it can't assume the case always returns 1. So I had to make it look like another boolean (real boolean, not 1 or 0), and then it worked. At least, it then ran into the field alias issue, which is now on the table.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39873
Joined: 17-Aug-2003
# Posted on: 19-May-2008 18:29:52   

Fixed in next build.

Frans Bouma | Lead developer LLBLGen Pro