IQuery

IQuery

强类型对象化查询接口,包含条件筛选、排序、分组查询、聚合查询、多表连接查询和分页等功能。

public interface IQuery<T> : IQuery
{
    /* 在上下文中跟踪实体 */
    IQuery<T> AsTracking();
    IEnumerable<T> AsEnumerable();
    /* 投影选择数据(支持匿名类型) */
    IQuery<TResult> Select<TResult>(Expression<Func<T, TResult>> selector);

    /* 筛选条件,和 linq 一样的用法 */
    IQuery<T> Where(Expression<Func<T, bool>> predicate);

    /* 排序,和 linq 一样的用法 */
    IOrderedQuery<T> OrderBy<K>(Expression<Func<T, K>> keySelector);
    IOrderedQuery<T> OrderByDesc<K>(Expression<Func<T, K>> keySelector);

    IQuery<T> Skip(int count);
    IQuery<T> Take(int count);
    /* 分页 */
    IQuery<T> TakePage(int pageNumber, int pageSize);

    /* 获取分组查询对象 */
    IGroupingQuery<T> GroupBy<K>(Expression<Func<T, K>> keySelector);

    IQuery<T> Distinct();

    /* 多表连接 */
    IJoiningQuery<T, TOther> Join<TOther>(JoinType joinType, Expression<Func<T, TOther, bool>> on);
    IJoiningQuery<T, TOther> Join<TOther>(IQuery<TOther> q, JoinType joinType, Expression<Func<T, TOther, bool>> on);

    /* 多表内连接 */
    IJoiningQuery<T, TOther> InnerJoin<TOther>(Expression<Func<T, TOther, bool>> on);
    /* 多表左连接 */
    IJoiningQuery<T, TOther> LeftJoin<TOther>(Expression<Func<T, TOther, bool>> on);
    /* 多表右连接 */
    IJoiningQuery<T, TOther> RightJoin<TOther>(Expression<Func<T, TOther, bool>> on);
    /* 多表全连接 */
    IJoiningQuery<T, TOther> FullJoin<TOther>(Expression<Func<T, TOther, bool>> on);

    /* 多表内连接 */
    IJoiningQuery<T, TOther> InnerJoin<TOther>(IQuery<TOther> q, Expression<Func<T, TOther, bool>> on);
    /* 多表左连接 */
    IJoiningQuery<T, TOther> LeftJoin<TOther>(IQuery<TOther> q, Expression<Func<T, TOther, bool>> on);
    /* 多表右连接 */
    IJoiningQuery<T, TOther> RightJoin<TOther>(IQuery<TOther> q, Expression<Func<T, TOther, bool>> on);
    /* 多表全连接 */
    IJoiningQuery<T, TOther> FullJoin<TOther>(IQuery<TOther> q, Expression<Func<T, TOther, bool>> on);

    T First();
    T First(Expression<Func<T, bool>> predicate);
    T FirstOrDefault();
    T FirstOrDefault(Expression<Func<T, bool>> predicate);

    /* 查询数据,返回 List 集合 */
    List<T> ToList();

    /* 判断满足条件的数据是否存在 */
    bool Any();
    bool Any(Expression<Func<T, bool>> predicate);

    /* 获取总条数 */
    int Count();
    long LongCount();

    /* 求最大值和最小值 */
    TResult Max<TResult>(Expression<Func<T, TResult>> selector);
    TResult Min<TResult>(Expression<Func<T, TResult>> selector);

    /* 求和 */
    int Sum(Expression<Func<T, int>> selector);
    int? Sum(Expression<Func<T, int?>> selector);
    long Sum(Expression<Func<T, long>> selector);
    long? Sum(Expression<Func<T, long?>> selector);
    decimal Sum(Expression<Func<T, decimal>> selector);
    decimal? Sum(Expression<Func<T, decimal?>> selector);
    double Sum(Expression<Func<T, double>> selector);
    double? Sum(Expression<Func<T, double?>> selector);
    float Sum(Expression<Func<T, float>> selector);
    float? Sum(Expression<Func<T, float?>> selector);

    /* 求平均值 */
    double Average(Expression<Func<T, int>> selector);
    double? Average(Expression<Func<T, int?>> selector);
    double Average(Expression<Func<T, long>> selector);
    double? Average(Expression<Func<T, long?>> selector);
    decimal Average(Expression<Func<T, decimal>> selector);
    decimal? Average(Expression<Func<T, decimal?>> selector);
    double Average(Expression<Func<T, double>> selector);
    double? Average(Expression<Func<T, double?>> selector);
    float Average(Expression<Func<T, float>> selector);
    float? Average(Expression<Func<T, float?>> selector);
}