ORDER BY IsNull(SomeField, 1) ASC

Posts   
 
    
mshe
User
Posts: 167
Joined: 02-Feb-2006
# Posted on: 05-Apr-2007 21:54:02   

Hi all,

I'm selecting the TOP 1 record in a table using a custom sort criteria. Is it possible to do an ORDER BY IsNull(Somefield, 1) ASC for sorting purposes?

If not - what is the best way to do this? Is there a way to append a calculated column to my result set?

I'm filling an EntityCollection, then telling the Adapter to only return 1 result.

Thanks!

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 06-Apr-2007 10:36:00   

The sort clause object accepts an EntityField, to which you can set the ExpressionToApply to a DBFunctionCall of IsNULL.

pat
User
Posts: 215
Joined: 02-Mar-2006
# Posted on: 09-May-2008 01:43:46   

Walaa wrote:

The sort clause object accepts an EntityField, to which you can set the ExpressionToApply to a DBFunctionCall of IsNULL.

Hi,

I'm just trying to do the same thing with LLBLGen 2.5 Adapter. Could you please give the example above in a few lines of code to help me understand it?

I like to have this SQL in the end:

SELECT VistorId, FirstName, LastName FROM Visitors ORDER BY ISNULL(VisitorName,LastName)

Thanks a lot, Patrick

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 09-May-2008 09:41:58   
EntityField myField = CustomerFields.VisitorName;
myField.ExpressionToApply = new DbFunctionCall("ISNULL", new object[] { VisitorFields.VisitorName, VisitorFields.LastName})

SortExpression sorter = new SortExpression(myField | SortOperator.Ascending)
pat
User
Posts: 215
Joined: 02-Mar-2006
# Posted on: 13-May-2008 03:34:19   

Thank you Waala.

For some reason this code:

EntityField2 myField = VisitorFields.VisitorName;
myField.ExpressionToApply = new DbFunctionCall("ISNULL", new object[] { VisitorFields.VisitorName, VisitorFields.VisitorId });

SortExpression sorter = new SortExpression(myField | SortOperator.Ascending);
EntityCollection<VisitorEntity> col = new EntityCollection<VisitorEntity>();
da.FetchEntityCollection(col, null, 0, sorter, prefetch);

returns this query:

SELECT [Test01].[dbo].[Visitor].[VisitorId], [Test01].[dbo].[Visitor].[VisitorName] FROM [Test01].[dbo].[Visitor]  ORDER BY [VisitorName] ASC

Any ideas?

Thanks, Patrick

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 13-May-2008 05:34:47   

The Walaa's advice is for TypedLists/DynamicLists. Here is why for EntityCollections is tricky, however also a workaround is provided: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=12394

David Elizondo | LLBLGen Support Team