Sort Ascending with NULLs at end of result

Posts   
 
    
geckoman7
User
Posts: 6
Joined: 23-Feb-2005
# Posted on: 23-Feb-2005 21:01:59   

I am trying to find a way to sort querys results ascending, but I want to have nulls appear at the end of the list. Does anyone know how to do this?

alexdresko
User
Posts: 336
Joined: 08-Jun-2004
# Posted on: 23-Feb-2005 21:48:35   

geckoman7 wrote:

I am trying to find a way to sort querys results ascending, but I want to have nulls appear at the end of the list. Does anyone know how to do this?

One way, which I agree isn't the best way, is to not allow nulls and set the default value of the field to be 9999999 (assuming you have an int field). The biggest problem with this solution is the assumption that you'll never have 9999999 records in your table. Though unlikely, I hate putting that kind of restriction in my code.

geckoman7
User
Posts: 6
Joined: 23-Feb-2005
# Posted on: 23-Feb-2005 21:54:25   

I think there is another way, but I am kind of new to LLBLGen. I think a more elegant way might be to create a custom property on the entity that takes to field that is used in the sort criteria, but when null is encountered it substitutes a value. Can this be done? Can you use the custom property like a field for sorting purposes? If so, are there any code examples available confused

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 24-Feb-2005 11:12:37   

ISNULL(field, value) is not supported at the moment... so substituting the value with 0 or something is not doable.

Another way to do this (in SQL that is) is using UNION. You can simulate a UNION by doing two entity collection fetches using the same filters, except the first is with an FieldCompareNull predicate where the field shouldn't be NULL, and the second, fetching into the same collection, is with the predicate filtering on the field should be NULL.

if you're using SelfServicing, be sure to set collection.SurpressClearInGetMulti to true before calling GetMulti() the second time.

Frans Bouma | Lead developer LLBLGen Pro
geckoman7
User
Posts: 6
Joined: 23-Feb-2005
# Posted on: 24-Feb-2005 17:07:23   

Thanks!