4

我正在尝试实现一种方法来检索表中的所有行,按 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; }
}
4

2 回答 2

5

没有答案?

好的,这就是我正在做的工作:

public static IEnumerable<PanelLog> GetPanelLogsDescendingSql(params object[] args)
{
    return me.db.Query<PanelLog>("select * from PanelLog ORDER BY ID DESC");        
}
于 2013-06-14T13:24:05.763 回答
0
return Table<T>().Select(i => i).OrderByDescending(xx => xx.ID).ToList();

或者

return me.db.Query<PanelLog>("select * from PanelLog ORDER BY ID DESC");

为我工作。

于 2021-03-30T18:25:47.620 回答