Sort by Is Not Null then Value

Posts   
 
    
Posts: 34
Joined: 08-Nov-2007
# Posted on: 06-Jun-2008 23:53:07   

I essentially want to sort by if the datetime column has a value and then by the actual value

        var q = from c in dbContext.User  
        where c.IsDisabled == false && c.IsSalesPool == true
        orderby c.LastAssignedContact.HasValue
                     orderby c.LastAssignedContact
        select c;

This throws an error, presumably since it can't map HasValue to a DB function?

Posts: 34
Joined: 08-Nov-2007
# Posted on: 06-Jun-2008 23:58:24   

I would have though the following would work: var q = from c in dbContext.User orderby (c.LastAssignedContact != null) orderby (c.LastAssignedContact) select c;

But it seems not

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 07-Jun-2008 08:36:01   

Make sure you use the latest build (RC released on june 5th). Also, 'throws an error' is too vague. As stated in the guidelines of this forum and in the beta docs: provide as much info as possible.

Also, you want to sort on a boolean expression. This isn't possible I think (would produce a CASE structure in the ORDER BY clause, I have to see if that's supported in SQL, I can imagine it's not).

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 07-Jun-2008 10:06:11   

I've checked and the query: select * from orders order by case when shipregion is null then 1 else 0 end

indeed works.

I'll build in support for a boolean in orderby clauses.

However, it doesn't really make that much sense: databases already sort nulls and non-nulls, and because the nulls and non-nulls are already grouped but all have the same value (null vs. non-null) the order of THOSE groups is undefined, so the result of such a query is the same as sorting on the field itself.

There's one difference: the way null values are threated is pre-defined per database: are they placed before or after values. With sorting on a boolean you can control this. And, of course you can also sort on boolean expressions which aren't related to null. I'll see what I can do.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 34
Joined: 08-Nov-2007
# Posted on: 07-Jun-2008 11:55:54   

Thanks Frans... Sorry for the vague report, wont happen again flushed

I've rework my design a little to eliminate this need, however as you say sorting on boolean expression where null might not be involved would be pretty kewl.

Cheers simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 07-Jun-2008 15:32:30   

Available in the next build (RTM)


var q = from o in metaData.Order
        where o.CustomerId == "BLAUS"
        orderby (o.ShippedDate.HasValue) descending, o.EmployeeId descending
        select o;

and


var q = from o in metaData.Order
        where o.CustomerId == "BLAUS"
        orderby (o.ShippedDate!=null) ascending, o.EmployeeId descending
        select o;

which means: every boolean expression (which ends up in a predicate/ filter in the sortclause) is handled.

Frans Bouma | Lead developer LLBLGen Pro