Collate SortExpression

Posts   
 
    
hotchill avatar
hotchill
User
Posts: 180
Joined: 22-Jan-2007
# Posted on: 02-Sep-2015 16:34:43   

Hi.

I am using - llblgen v4.2 Final, April 10th, 2015 - adapter pattern - sql server 2012 R2

I want to achieve this:

select * from [process] order by [Title] collate Danish_Norwegian_CI_AS asc

I tried with this, but it does not work

var collate = new DbFunctionCall("{0} collate Danish_Norwegian_CI_AS", new object[] { ProcessFields.Title });
adapter.FetchEntityCollection(processes, filter, -1, new SortExpression(ProcessFields.Title.SetExpression(collate) | SortOperator.Ascending), prefetch);

Any suggestions?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 02-Sep-2015 22:25:52   

You need to use a SortClause and set its EmitAliasForExpressionAggregateField property to false. Please check the code sample here: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=14331

hotchill avatar
hotchill
User
Posts: 180
Joined: 22-Jan-2007
# Posted on: 03-Sep-2015 13:04:29   

Thanks Walaa. I encapsulated this in utility methods passing lcid for future use.

public static SortExpression GetCollateSortExpression(EntityField2 field, SortOperator sortOperator, int lcid) {
    return new SortExpression(GetCollateSortClause(field, sortOperator, lcid));
}

public static SortClause GetCollateSortClause(EntityField2 field, SortOperator sortOperator, int lcid) {
    var sortField = new EntityField2(string.Format("collate_{0}", field.Name), new DbFunctionCall("{0} collate Danish_Norwegian_CI_AS", new object[] { field }));
    return new SortClause(sortField, null, sortOperator) { EmitAliasForExpressionAggregateField = false };
}