2011-12-17 12 views
5

Devo ordinare il risultato nella tabella DB ChargeOperations in la mia direzione entro il typeId. La richiesta SQL è come questo:Come utilizzare il caso e ordinare in Nhibere?

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 

Quindi, per favore, mi puoi dare un esempio di come posso creare questa costruzione.

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 

con QueryOver.

+0

è 'TypeId' il valore discriminatore? hai bisogno di Paging? – Firo

+0

Non conosco il tuo significato come valore discriminatore. TypeId è un campo per l'ordinamento. Non ho bisogno di Paging o di altro. So come crearlo con HQL, ma usiamo queryOver ... –

risposta

2

Si potrebbe fare utilizzando il Projections.Conditional, per il campione:

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(); 
+0

anche se sono domande molto vecchie, lascia che sia la risposta :) Non riesco a controllarlo. –

Problemi correlati