简体   繁体   中英

Case-insensitive sort ordering in NHibernate

Consider the following criteria query:

var x = SomeCriteria.AddOrder(new Order("Name", true)).List();

This will order the result set by the Name property, but case sensitive:

"A1"
"B1"
"a2"

Any ideas how to add the order case insensitive so result "a2" will end up before "B1"?

You should be able to accomplish this by ordering on a projection that normalizes the case for you. For example, Oracle has a "lower" function that will lower case string data types like varchar2 and nvarchar2; so I will use this sql function to form a projection that will order appropriately.

var projection = Projections.SqlFunction("lower", 
                                         NHibernateUtil.String, 
                                         Projections.Property("Name"));

var x = SomeCriteria.AddOrder(Orders.Asc(projection)).List()

If you're using SQL Server, I'd recommend using the "upper" function instead of "lower" for efficiency. Microsoft has optimized its native code for performing uppercase comparisons, where the rest of the world seems to have optimized on lowercase.

Hibernate (Java) has an "ignoreCase()" method on the "Order" class, but it looks like NHibernate does not have this method on its "Order."

This is how I was thinking you could do it:

var x = SomeCriteria.AddOrder(new Order("Name", true).IgnoreCase()).List();

But unfortunately, there is no IgnoreCase().

As a workaround, you could use an HQL or SQL query - either of those should allow you to order case-insensitive.

This probably depends on a case-sensitivity setting on your database server. I suspect that NHibernate just issues an "ORDER BY" clause; at least, I can't imagine what else it would do. For SQL Server, the default sort order (collation) is dictionary order, case insensitive.

This article gives some techniques for performing case sensitive searches in SQL Server. However, my advice is to sort the list that is returned by the query in code. That solution preserves the database independence of NHibernate and let's you customize the sort order per your needs.

As I know the responses to my query are always fairly small, I ended up querying the data as normal and sorting them afterwards using Linq. It works, so why bother tweaking NHibernate ;) (Using SQLite, btw)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM