Belius' Blog

Il blog di Simone Belia
posts - 16, comments - 335, trackbacks - 31

[UPDATE] NHibernate complex queries

Pochi giorni fa avevo scritto un post  su come creare degli oggetti che incapsulassero le informazioni per filtrare dei dati in NH (e non solo).

Questo post non è altro che una piccolissima aggiunta con l'implementazione di una classe che rappresenta l'ordine dei risultati della query.

Il concetto è quello di avere una classe che contenga il campo da ordinare e la direzione di ordinamento (espressa tramite un enum).

public class OrderField
{
     
private string _field = string.Empty;
     private 
OrderDirection _direction OrderDirection.Asc;

     public string 
Field
     {
         
get return _field}
         
set { _field = value; }
     }

     
public OrderDirection Direction
     {
         
get return _direction}
         
set { _direction = value; }
     }

     
public OrderField(string field, OrderDirection direction)
     {
         _field 
field;
         
_direction direction;
     
}
}

 

public enum OrderDirection
{
      Asc,
      Desc
}
 
Visto che potrebbero verificarsi numerosi casi in cui un solo campo non basta ai fini dell'ordinamento, ho pensato fosse utile implementare una collezione che dasse la possibilità di recuperare in un colpo tutta la stringa di ordinamento.

 

public class OrderFieldList: List<OrderField>
{
      
public override string ToString()
      {
          StringBuilder sb 
= new StringBuilder("");
          if 
(this.Count >0)
          {
              sb.Append(
"ORDER BY ");
              foreach 
(OrderField of in this)
              {
                  sb.AppendFormat(
"{0} {1},", of.Field, of.Direction.ToString());
              
}

              sb.Remove(sb.Length-
11);
          
}

          
return sb.ToString();
     
}
}
 
 
 ...e per finire la sua implementazione ai fini pratici è:
 
string hql "select c from Customer c where c.Country = :country"

                
OrderFieldList ofl = new OrderFieldList();
ofl.Add(new OrderField("BirthDate", OrderDirection.Desc));
ofl.Add(new OrderField("LastName", OrderDirection.Asc));

hql +ofl.ToString();

//..........
//..........
//..........
....a presto!!!

Print | posted on martedì 16 ottobre 2007 16:37 | Filed Under [ ASP.NET Tips ]

Feedback

Gravatar

# re: [UPDATE] NHibernate complex queries

PDF to TIFF Converter is a professional PDF to images program which let you convert PDF to TIFF and lots of image formats with high quality, such as JPEG, PNG, GIF, BMP, PCX, TGA. As to convert PDF to TIFF, you can freely set output encoder, including lossless, FAX G3, etc. Using this PDF to TIFF Converter, you alos can choose colorful, grey and black/white as output color. Reset resolution is applicable as well.
PDF to XLS Converter
PDF to EPUB Converter
Convert PDF to GIF
19/08/2010 09:30 | taoruankeji
Gravatar

# re: [UPDATE] NHibernate complex queries

Education aims to give you a boost up the ladder of knowledge. Too often, it just gives you a cramp on one of its rungs.
30/03/2012 20:30 | dissertation Introduction
Gravatar

# re: [UPDATE] NHibernate complex queries

If you are willing to enlarge your knowledge close to this good post, find thesis writing service or buy dissertation service and order perfect thesis mba over there.
13/04/2012 10:12 | KirstenMarquez
Gravatar

# re: [UPDATE] NHibernate complex queries

I think it's affecting, because it exhibit a very academic-focussed attitude. Preserving the honestness of the academic system seems to be a precedence, although directly close down remunerated ads for such a service seems a flaccid response. When the enterprise is known by your friends who were enlivened with the results of the association, about this post . But remember always to use plagiarism checking run them through this plagiarism detection system for absolute checking and make sure that your material is authentic.
23/04/2012 17:26 | plagiarism checking
Gravatar

# re: [UPDATE] NHibernate complex queries

Buy term papers online written in accordance with your requirements, and make more free time for having time of your life.
26/05/2012 06:00 | custom term paper
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET