Skip to content

OrderBy calc expressions support #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions examples/Stocks/Stocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ public Stock QueryStock (string stockSymbol)
}
public IEnumerable<Stock> QueryAllStocks ()
{
return from s in Table<Stock> ()
orderby s.Symbol
select s;
return Table<Stock>().OrderBy(s => s.Symbol.ToLower());
}

public void UpdateStock (string stockSymbol)
Expand Down
3 changes: 3 additions & 0 deletions src/SQLite.Net/BaseTableQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,6 +32,8 @@ protected class Ordering
[CanBeNull]
public string ColumnName { get; set; }

public Expression OrderByExpression { get; set; }

public bool Ascending { get; set; }
}
}
Expand Down
38 changes: 27 additions & 11 deletions src/SQLite.Net/TableQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,32 +227,47 @@ private TableQuery<T> AddOrderBy<TValue>([NotNull] Expression<Func<T, TValue>> 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<T>();
if (q._orderBys == null)
{
q._orderBys = new List<Ordering>();
}
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;
}

Expand Down Expand Up @@ -314,8 +329,9 @@ private SQLiteCommand GenerateCommand([NotNull] string selectionList)
}
if ((_orderBys != null) && (_orderBys.Count > 0))
{
List<object> orderByArgs = new List<object>();
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)
Expand Down