3

有没有办法使用 GetAllWithChildren() 或 GetAll() 或 GetAllWithChildrenAsync() 或 GetAllAsync() 来限制/分页结果集。

这些方法接受过滤表达式,但似乎没有任何直接的方法可以设置显式限制器或 orderby 子句。

4

1 回答 1

4

GetAllWithChildren方法只是一种方便的方法。要执行自定义查询,您必须使用该Table方法。使用您想要扩展现有功能的功能来实现您自己的 fetch 方法应该不难GetAllWithChildren

public static class OrderExtensions {
    public static List<T> GetAllWithChildren<T>(this SQLiteConnection conn,
            Expression<Func<T, bool>> filter = null,
            Expression<Func<T, object>> orderExpr = null,
            int? limit = null,
            int? offset = null,
            bool recursive = false) where T: class
    {
        var elements = conn.Table<T>();
        if (filter != null) {
            elements = elements.Where(filter);
        }
        if (orderExpr != null) {
            elements = elements.OrderBy(orderExpr);
        }
        if (offset != null) {
            elements = elements.Skip(offset.Value);
        }
        if (limit != null) {
            elements = elements.Take(limit.Value);
        }

        var list = elements.ToList();

        foreach (T element in list)
        {
            conn.GetChildren(element, recursive);
        }

        return list;
    }
}

然后你可以像这样使用它:

    conn.GetAllWithChildren<MyClass>(
            filter: o => o.Name != "",
            orderBy: o => o.Name,
            limit: 10, offset: 20);

或不那么冗长(且描述性较差)的版本:

    conn.GetAllWithChildren<MyClass>(o => o.Name != "", o => o.Name, 10, 20);
于 2015-12-22T09:53:53.947 回答