.net中的SQLite中不區分大小寫的順序 (case insensitive order by in SQLite in .net)


問題描述

.net中的SQLite中不區分大小寫的順序 (case insensitive order by in SQLite in .net)

I'm using SQLite from a C# program using SQLite.net ( http://sqlite.phxsoftware.com ).

By default SQLite select order by clause sort is case sensitive, I want the result to sort case insensitive, I found "COLLATE NOCASE" but the documentation says it will only handle English characters in the ascii range, I want true linguistic international case insensitive sort using the CultureInfo.CurrentCulture collation (making it use String.Compare will do the trick).


參考解法

方法 1:

I believe such collation is not provided in current versions of SQLite. As such it would seem that the most sensible plan is to remove the sort from the query and instead sort afterwards in pure .Net where you have full control and access to constructs like the thread's culture info.

Since both are happening in the same process this shouldn't make a big difference in performance terms unless your dataset is very large.

SQLite 3 does allow user defined collation functions, and these can be done in SQLite.Net as .net functions but the overhead of calling back and forth across the managed/unmanaged boundary is considerable. Here is one persons attempts to do it in c++. Unless you have access to someone else's well tested and stable unicode culture sensitive sort in C++ I suggest sticking to the simple sort after approach where possible.

Of course if the performance of the user defined collation is more than enough for your present needs then go with that.

[SQLiteFunction(Name = "CULTURESORT", FuncType = FunctionType.Collation)]
class CultureSort : SQLiteFunction
{
    public override int Compare(string param1, string param2)
    {
        return String.Compare(
            param1,param2, CultureInfo.CurrentCulture, CompareOptions.IgnoreCase)
        );
    }
}

Post script if you fancy getting fancy: There is a blessed build of SQLite which integrates the ICU library for 'proper' unicode support on ordering/like/upper/lower but you would need to integrate that into the sqlite code used as the backing for the .Net wrapper. This many not be easy. 

方法 2:

SQLite.NET allows you to define custom functions in C# that you can use in SQL queries. So it should be possible to write a custom_upper or custom_lower function in C# that would handle the case conversion with the current culture collation...

方法 3:

You can simply use the lower() sqlite function:

select * from data order by lower(name)

方法 4:

Aye, this is much more complex that you would guess it is.  In some cultures the rules are different and without know what culture you want to base it off of it becomes impossible.  Consider the famous (amoung i18n people) example of Turkish.

(by NirShuggyCoUkThomas Levesquenjzk2JasonRShaver)

參考文件

  1. case insensitive order by in SQLite in .net (CC BY-SA 3.0/4.0)

#sql-order-by #sqlite #.net #case-insensitive






相關問題

如果提到,按特定值自定義 SQL 記錄集順序 (Customize SQL recordset order by specific value (s) if mentioned)

SQL Server 中的 OrderBy 將正值放在負值之前 (OrderBy in SQL Server to put positive values before negative values)

Đếm từ hai bảng và sắp xếp nó theo số lượng SQL tồn tại (Count from two tables and sort it out by the number of exist SQL)

按組中的最大值排序 (Order by the max value in the group)

.net中的SQLite中不區分大小寫的順序 (case insensitive order by in SQLite in .net)

MySQL:對 GROUP_CONCAT 值進行排序 (MySQL: Sort GROUP_CONCAT values)

為 MySQL 選擇查詢自定義 order by (Customize order by for MySQL select query)

如何從 MySQL 表中檢索 10 個最新的 DISTINCT IP? (How to retrieve 10 latest DISTINCT IP's from a MySQL table?)

按存儲在文本列中的日期排序表 (Order table by date stored in text column)

如何在 JavaScript 中對對象集合進行自然排序? (How to natural-sort a collection of objects in JavaScript?)

在 Postgres 中通過 json 數組(存儲為 jsonb)中的鍵對錶進行排序 (Order a table by a key in an array of json (stored as jsonb) in Postgres)

添加 row_number() 時 Oracle 值發生變化 (Oracle values change when row_number() added)







留言討論