sqlite - 如何使用 SQLite.NET 按降序排序? OrderByDescending() 给我错误 : "Order By does not support: xx => Convert(xx).ID"

标签 sqlite xamarin.ios xamarin.android

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

最佳答案

没有答案?

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

public static IEnumerable<PanelLog> GetPanelLogsDescendingSql(params object[] args)
{
    return me.db.Query<PanelLog>("select * from PanelLog ORDER BY ID DESC");        
}

关于sqlite - 如何使用 SQLite.NET 按降序排序? OrderByDescending() 给我错误 : "Order By does not support: xx => Convert(xx).ID",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17104378/

相关文章:

sqlite - Gradle项目中的SQLite

c# - Xamarin PCL 中缺少 Type.GetProperties()

ios - 了解何时有人购买了您的应用

android - 使用 ObtainStyledAttributes 的 monodroid/xamarin 自定义属性为空

xamarin.ios - ServiceStack 可以支持 websockets 吗?

android - SQlite 或共享首选项

c# - 如何使用 sqlite-net 在适用于所有数据库模型的父类中实现插入、更新和删除?

ruby - 无法在运行 rails 3.2.11 的 Ruby 2 中加载此类文件 - sqlite3/sqlite3_native (LoadError)

c# - 在 Xamarin.Forms 中使用带有嵌入图像的图标属性

c# - 当我使用非共享运行时构建 Monodroid 项目时获取编码失败