Sorting on custom entity field

Posts   
 
    
madrigal
User
Posts: 3
Joined: 15-Sep-2009
# Posted on: 23-Sep-2009 10:05:33   

Hi,

I've added custom field (LastCounterValue) to my CarEntity writing custom entity factory and overriding CreateFields() method. Everything works fine except sorting on that field.

Below there is generated query

SELECT [TMS].[dbo].[Car].[IDCar], 
[TMS].[dbo].[Car].[CarBrand], 
[TMS].[dbo].[Car].[VehicleRegistration], 

ISNULL((SELECT MAX([TMS].[dbo].[MRFuelReport].[CounterValue]) AS [CounterValue] 
        FROM [TMS].[dbo].[MRFuelReport]  
        WHERE ( [TMS].[dbo].[Car].[IDCar] = [TMS].[dbo].[MRFuelReport].[IDCar])), [TMS].[dbo].[Car].[CounterFirstEntry]) 
        AS [LastCounterValue] 
        
FROM ( [TMS].[dbo].[Car]  LEFT JOIN [TMS].[dbo].[MRFuelReport]  
    ON  [TMS].[dbo].[Car].[IDCar]=[TMS].[dbo].[MRFuelReport].[IDCar]) 
ORDER BY [].[LastCounterValue] ASC

Problem is with object alias in ORDER BY clause ([].[LastCounterValue]). Object alias shouldn't be generated, is it possible to do that?

Thanks,

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 23-Sep-2009 11:55:47   

Which LLBLGen Pro runtime library build are you using? Would you please post the SortExpression code?

Would you please check the following thread, which seems relevant to your issue: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=16406

madrigal
User
Posts: 3
Joined: 15-Sep-2009
# Posted on: 23-Sep-2009 12:19:21   

I'm using 2.6.0.0 version.

Here is my SortExpression code:

EntityField2 sortField = new EntityField2("LastCounterValue", null);
ISortExpression sortExp  = new SortExpression(sortField |  SortOperator.Descending);

And here is my entity factory class:

public class ExtendedCarEntityFactory : CarEntityFactory
    {
        public override IEntityFields2 CreateFields()
        {
            IEntityFields2 toReturn = base.CreateFields();
            toReturn.Expand(1);


            EntityField2 lastCounterValueField = new EntityField2("LastCounterValue", 
                new DbFunctionCall(
                    "ISNULL", 
                    new object[]{new ScalarQueryExpression( MRFuelReportFields.CounterValue.SetAggregateFunction(AggregateFunction.Max) ,(CarFields.IDCar == MRFuelReportFields.IDCar)),
                        CarFields.CounterFirstEntry}
                    ));

            toReturn.DefineField(lastCounterValueField, toReturn.Count - 1);
            return toReturn;
        
        }
    }
madrigal
User
Posts: 3
Joined: 15-Sep-2009
# Posted on: 23-Sep-2009 12:37:46   

I've solved it,

I have modified creation of sortFiled to the same like in ExtendedCarEntityFactory and everything works fine.


sortField = new EntityField2("LastCounterValue",
                                                        new DbFunctionCall(
                                                            "ISNULL",
                                                            new object[]{new ScalarQueryExpression( MRFuelReportFields.CounterValue,(CarFields.IDCar == MRFuelReportFields.IDCar),null,new SortExpression( MRFuelReportFields.RefuelStamp |SortOperator.Descending),null,true),
                                                                CarFields.CounterFirstEntry}
                                                            ));

ISortExpression sortExp = new SortExpression(sortField | SortOperator.Descending);