Sort on SQL native function?

Posts   
 
    
DarinSh
User
Posts: 9
Joined: 25-Feb-2007
# Posted on: 25-Feb-2007 00:36:35   

I'm using version 1.0.2005 of llbl, and would like to sort on a sql server function, rather than a field entity. The idea is that I want to return a random set of rows from a fairly small table. To accomplish that, my ideal sql would look like this..


Select TOP 20 * from MyTable
Order by newid()

I cant see how to create a sort expression on something other than a field entity.

Can this be done?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Feb-2007 06:14:14   

I cant see how to create a sort expression on something other than a field entity. Can this be done?

Sorting is the ability to order data in one or more fields ascending (A -> Z) or descending (Z -< A). You do this by constructing a SortExpression with one or more SortClauses. SortClauses are simple definitions which contain information about **which field to sort **and in which direction (ascending/descending).

As I see, there is no way, even in 2.0 version (you have an older one).

However,

Select TOP 20 * from MyTable
Order by newid()

Did you test that? I did and have no order effect. Only with constants (1,2,3, etc.)

I recommend you to use two DB calls. One for a DbFunctionCall and another to fetch collection.

David

David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 25-Feb-2007 12:19:03   

Actually, with a trick you can simple_smile Here's my unittest which shows how to fetch random rows from sqlserver: (note: this is 1.0.2005.1 specific code. )


[Test]
public void RandomFetchTest()
{
    DataAccessAdapter adapter = new DataAccessAdapter();

    try
    {
        IEntityField2 newIDField = EntityFieldFactory.Create(CustomerFieldIndex.Country);
        newIDField.Name = "NEWID";
        newIDField.Alias = "NEWID()";
        newIDField.ExpressionToApply = new Expression();

        ISortExpression sorter = new SortExpression(new SortClause(newIDField, null, SortOperator.Ascending));
        EntityCollection customers = new EntityCollection(new CustomerEntityFactory());
        adapter.FetchEntityCollection(customers, null, 0, sorter);
        ResultsetViewer viewer = new ResultsetViewer();
        viewer.BindCollection(customers); 
        viewer.SetTestPurpose("RandomFetchTest: fetches random customers");
        viewer.ShowDialog(null);
    }
    finally
    {
        adapter.Dispose();
    }
}

For v2.0, you need this:


[Test]
public void RandomFetchTest()
{
    DataAccessAdapter adapter = new DataAccessAdapter();

    try
    {
        IEntityField2 newIDField = EntityFieldFactory.Create(CustomerFieldIndex.Country);
        newIDField.Alias = "NEWID()";
        newIDField.ExpressionToApply = new Expression();

        ISortExpression sorter = new SortExpression(new SortClause(newIDField, null, SortOperator.Ascending));
        EntityCollection<CustomerEntity> customers = new EntityCollection<CustomerEntity>(new CustomerEntityFactory());
        adapter.FetchEntityCollection(customers, null, 0, sorter);

        ResultsetViewer viewer = new ResultsetViewer();
        viewer.BindCollection( customers );
        viewer.SetTestPurpose( "RandomFetchTest: fetches random customers" );
        viewer.ShowDialog( null );
    }
    finally
    {
        adapter.Dispose();
    }
}

Frans Bouma | Lead developer LLBLGen Pro
DarinSh
User
Posts: 9
Joined: 25-Feb-2007
# Posted on: 26-Feb-2007 22:14:55   

Worked like a charm! Thanks Frans!