TOP and ORDER BY

Posts   
 
    
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 09-Apr-2009 02:41:21   

Oh silly me, I've just discovered that if you use LLBLGen paging whilst sorting on a non-unique column then the results come out wrong. I can't believe I haven't noticed this before.

I've looked at the generated T-SQL and the problem seems to be with using TOP and ORDER BY as described here...

http://blogs.technet.com/wardpond/archive/2008/06/26/top-order-by-and-non-unique-columns.aspx

So, I figure a solution is to just add an extra sort clause of the entity's primary key. How can I automatically add an extra sort clause on the primary key when given an instance of 'PerformSelectEventArgs2'? And couldn't this be done by LLBLGen automatically? So if the sort column(s) aren't unique then add the pk?

Cheers, Ian.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 09-Apr-2009 03:50:18   

You have to specify the sorter. If you are using LivePersistence=true (you dont have handlers for PerformSelect and PerformWork), you can do this, for instance, at Page_Load:

if (!IsPostBack)
{
    // if you want to add the clasue
     myLLBLGenProDataSource.SorterToUse.Add(.....);

     // if you want to create a new one
     myLLBLGenProDataSource.SorterToUse = new SortExpresion(....);
}

If you are using LivePersistence=false, add the sort clause at your PerformSelect event handler.

David Elizondo | LLBLGen Support Team
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 09-Apr-2009 04:08:37   

If you are using LivePersistence=false, add the sort clause at your PerformSelect event handler.

Is it possible to look up the primary key from the 'args' parameter without knowing what type the collection contains? You see the GridView that I'm binding to is used to display entity collections where the entity's type is arbitrary.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 09-Apr-2009 11:52:25   

without knowing what type the collection contains?

How do you set the type at runtime? There should be a place in code where the type gets known.

Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 09-Apr-2009 14:57:28   

There should be a place in code where the type gets known.

There isn't! The type is just a string passed to the datasource. It comes out of the database and is never examined.


string factoryTypeName =
                string.Concat("JGSR.BFWeekender.DataAccessServices.FactoryClasses.", TypeName,
                              "EntityFactory, JGSR.BFWeekender.DataAccessServices");

            llblGenDataSource.EntityFactoryTypeName = factoryTypeName;

I could add an additional column in the db to store the pk field but it seems a waste when this information is embedded in the LLBLGen generated code.

Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 16-Apr-2009 04:34:17   

Any chance that LLBLGen could automatically add a sort by the primary key if a user is paging and sorting with a non-unique column?

Doesn't forcing LLBLGen's users to deal with this break LLBLGen's encapulation? Knowing that one needs sort on a unique field when paging depends on knowing how LLBLGen implements paging.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 16-Apr-2009 09:15:59   

No, paging always depends on ordering. A query without an ordering statement (order by) is an unordered set, by definition (SQL standard), and we won't add an ordering to it, you have to do that yourself.

Frans Bouma | Lead developer LLBLGen Pro