Looks like the method wasn't virtual in earlier versions, I used the one from May. Just upgraded and changed frem "new" to "override".
It looks like the Expression only get applied to the selectfields and not the group by statement.
My Code:
using System;
using SD.LLBLGen.Pro.ORMSupportClasses;
namespace LLBLgenExt {
[Serializable]
public class FunctionExpression:Expression {
#region class member declarations
private string _functionToApply;
#endregion
public FunctionExpression():base() {
_functionToApply = string.Empty;
}
/// Selfservicing constructor.All other constructors are hidden, we only work on a single field.
public FunctionExpression(IEntityField field, string functionToApply) : base(field) {
_functionToApply = functionToApply;
}
/// Adapter constructor. All other constructors are hidden, we only work on a single field.
public FunctionExpression(IEntityField2 field, string functionToApply) : base(field) {
_functionToApply = functionToApply;
}
public override string ToQueryText(ref int uniqueMarker, bool inHavingClause) {
if(_functionToApply.Length>0) {
return string.Format("{0}({1})", _functionToApply, base.ToQueryText(ref uniqueMarker, inHavingClause));
}
else {
return base.ToQueryText(ref uniqueMarker, inHavingClause);
}
}
#region properties
public string FunctionToApply {
get { return _functionToApply;}
set { _functionToApply = value;}
}
#endregion
}
}
And implementation:
public DataTable GetArchiveContactCharCount( int companyGuid, int[] archiveGroupGuids){
ResultsetFields fields = new ResultsetFields(1);
fields.DefineField( BenefitArchiveContactFieldIndex.Firstname,0,"charsCount");
IGroupByCollection groupByClause = new GroupByCollection();
groupByClause.Add(fields[0]);
IExpression exp = new FunctionExpression(
EntityFieldFactory.Create(BenefitArchiveContactFieldIndex.Firstname),"len");
fields[0].ExpressionToApply=exp;
DataTable dynamicList = new DataTable();
TypedListDAO dao = new TypedListDAO();
dao.GetMultiAsDataTable(fields, dynamicList,0,null, null, null, true, groupByClause, null, 0, 0);
return dynamicList;
}
This gives the following sql query:
SELECT len([dbo].[TBLbenefitArchiveContact].[firstname]) AS [charsCount] FROM [dbo].[TBLbenefitArchiveContact] GROUP BY [dbo].[TBLbenefitArchiveContact].[firstname]
Do you have any suggestions?