5

I need to order result in DB table ChargeOperations in my own direction by typeId. The SQL request is like this:

SELECT * FROM ChargeOperations co
LEFT JOIN ShadowChargeOperations sco ON sco.ChargeOperationId=co.Id
-- just exclude some extra data.
WHERE sco.Id IS NULL
ORDER BY
 CASE co.TypeId
  WHEN 1 THEN 3   -- this is my order, which is different from id of type and can change
  WHEN 2 THEN 1
  WHEN 3 THEN 2
  ELSE 4
 END,
 co.TypeId,
 co.CalculationAmount

So, please, can you give me an example of how can I create this construction.

CASE co.TypeId 
  WHEN 1 THEN 3   -- this is my order, which is different from id of type and can change
  WHEN 2 THEN 1
  WHEN 3 THEN 2
  ELSE 4

with QueryOver.

Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
Artem A
  • 2,154
  • 2
  • 23
  • 30
  • is `TypeId` the discriminator value? do you need Paging? – Firo Dec 19 '11 at 09:02
  • I don't know you meaning as discriminator value. TypeId is a field for sorting. I don't need Paging or smth else. I know how to create it with HQL, but we use queryOver... – Artem A Dec 19 '11 at 09:07

1 Answers1

4

You could do it using the Projections.Conditional, for sample:

ChargeOperation itemAlias = null;

var result = 
    session.QueryOver<ChargeOperation>(() => itemAlias)
            .Where ( /*your conditions*/)
            .OrderBy(Projections.Conditional(
                        Restrictions.Where(() => itemAlias.TypeId == 1),
                        Projections.Constant(3),                                
                    Projections.Conditional(
                        Restrictions.Where(() => itemAlias.TypeId == 2),
                        Projections.Constant(1),
                    Projections.Conditional(
                        Restrictions.Where(() => itemAlias.TypeId == 3),
                        Projections.Constant(2),
                        )
                    )           
                )                           
            ).Asc
            .ThenBy(x => x.TypeId)
            .ThenBy(x => x.CalculationAmount)
        .List();
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194