Sorty BY len(FIeld)

Posts   
 
    
Anthony
User
Posts: 155
Joined: 04-Oct-2006
# Posted on: 20-Sep-2008 02:40:15   

dotnet2,llblgen2.6,adapter mode,c#,access db

How do i implement this in code?

SELECT Name FROM tblName ORDER BY len(Name)

I can do this but have no idea how to do the sort.

thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 21-Sep-2008 19:50:53   

You should use DBFunctionCall, set this expression on a field and pass it in the sortclause:

// define the field you want to retrieve
ResultsetFields fields = new ResultsetFields(1);
fields.DefineField(CustomerFields.ContactName, 0);

// The Sorter
EntityField2 sortField = new EntityField2("theLen", new DbFunctionCall("LEN", new object[] { CustomerFields.ContactName }));
SortClause lenSortClause = new SortClause(sortField, null, SortOperator.Ascending);
// this is important, setting this to false will emit our custom expression at ORDER BY clause
lenSortClause.EmitAliasForExpressionAggregateField = false;
SortExpression sorter = new SortExpression(lenSortClause);

// fetch the resuts
DataTable results = new DataTable();
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
    adapter.FetchTypedList(fields, results, null, 0, sorter, true);
}

You also can do that using EntityCollection instead of DynamicList.

Hope helpful simple_smile

David Elizondo | LLBLGen Support Team