我正在尝试实现一种方法来检索表中的所有行,按 ID 降序排序。
这是代码:
public IEnumerable<T> GetItemsDescending<T>() where T : IBusinessEntity, new()
{
lock (locker)
{
return Table<T>().Select(i => i).OrderByDescending(xx => xx.ID).ToList();
}
}
这看起来应该可以工作,但是我遇到了一个我不明白的错误:
"Order By does not support: xx => Convert(xx).ID"
从以下 SQLite 方法:
private TableQuery<T> AddOrderBy<U>(Expression<Func<T, U>> orderExpr, bool asc)
{
if (orderExpr.NodeType == ExpressionType.Lambda)
{
var lambda = (LambdaExpression)orderExpr;
MemberExpression mem = null;
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))
{
var q = Clone<T>();
if (q._orderBys == null)
{
q._orderBys = new List<Ordering>();
}
q._orderBys.Add(new Ordering
{
ColumnName = Table.FindColumnWithPropertyName(mem.Member.Name).Name,
Ascending = asc
});
return q;
}
else
{
throw new NotSupportedException("Order By does not support: " + orderExpr);
}
}
else
{
throw new NotSupportedException("Must be a predicate");
}
}
根据要求:PanelLog
课程:
public class PanelLog : IBusinessEntity
{
public PanelLog()
{
}
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public uint Sequence { get; set; }
public DateTime Time { get; set; }
public string Message { get; set; }
public bool Alarm { get; set; }
}