diff --git a/examples/Stocks/Stocks.cs b/examples/Stocks/Stocks.cs index cac8cc177..d986ea23d 100644 --- a/examples/Stocks/Stocks.cs +++ b/examples/Stocks/Stocks.cs @@ -63,9 +63,7 @@ public Stock QueryStock (string stockSymbol) } public IEnumerable QueryAllStocks () { - return from s in Table () - orderby s.Symbol - select s; + return Table().OrderBy(s => s.Symbol.ToLower()); } public void UpdateStock (string stockSymbol) diff --git a/src/SQLite.Net/BaseTableQuery.cs b/src/SQLite.Net/BaseTableQuery.cs index d3111d333..93aef1d21 100644 --- a/src/SQLite.Net/BaseTableQuery.cs +++ b/src/SQLite.Net/BaseTableQuery.cs @@ -19,6 +19,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. +using System.Linq.Expressions; using JetBrains.Annotations; namespace SQLite.Net @@ -31,6 +32,8 @@ protected class Ordering [CanBeNull] public string ColumnName { get; set; } + public Expression OrderByExpression { get; set; } + public bool Ascending { get; set; } } } diff --git a/src/SQLite.Net/TableQuery.cs b/src/SQLite.Net/TableQuery.cs index 96c4f989a..ee02b308f 100644 --- a/src/SQLite.Net/TableQuery.cs +++ b/src/SQLite.Net/TableQuery.cs @@ -227,32 +227,47 @@ private TableQuery AddOrderBy([NotNull] Expression> o } var lambda = (LambdaExpression) orderExpr; - MemberExpression mem; + MemberExpression mem = null; + Expression methodCallExpression = null; - var unary = lambda.Body as UnaryExpression; - if (unary != null && unary.NodeType == ExpressionType.Convert) + if (lambda.Body is MethodCallExpression) { - mem = unary.Operand as MemberExpression; + methodCallExpression = lambda.Body; } else { - mem = lambda.Body as MemberExpression; + var unary = lambda.Body as UnaryExpression; + if (unary != null && unary.NodeType == ExpressionType.Convert) + { + mem = unary.Operand as MemberExpression; + } + else + { + mem = lambda.Body as MemberExpression; + } } - if (mem == null || (mem.Expression.NodeType != ExpressionType.Parameter)) + if ((mem == null || mem.Expression.NodeType != ExpressionType.Parameter) && methodCallExpression == null) { throw new NotSupportedException("Order By does not support: " + orderExpr); } + var q = Clone(); if (q._orderBys == null) { q._orderBys = new List(); } - q._orderBys.Add(new Ordering + + var ordering = new Ordering { - ColumnName = Table.FindColumnWithPropertyName(mem.Member.Name).Name, - Ascending = asc - }); + Ascending = asc, + OrderByExpression = methodCallExpression + }; + + if (mem != null) + ordering.ColumnName = Table.FindColumnWithPropertyName(mem.Member.Name).Name; + + q._orderBys.Add(ordering); return q; } @@ -314,8 +329,9 @@ private SQLiteCommand GenerateCommand([NotNull] string selectionList) } if ((_orderBys != null) && (_orderBys.Count > 0)) { + List orderByArgs = new List(); var t = string.Join(", ", - _orderBys.Select(o => "\"" + o.ColumnName + "\"" + (o.Ascending ? "" : " desc")).ToArray()); + _orderBys.Select(o => (o.OrderByExpression != null ? CompileExpr(o.OrderByExpression, orderByArgs).CommandText : ("\"" + o.ColumnName + "\"")) + (o.Ascending ? "" : " desc")).ToArray()); cmdText += " order by " + t; } if (_limit.HasValue)