Exemplo n.º 1
0
        /// <summary>
        /// Extendable method: 过滤数据源中属性包含(或不包含)某些值的实体(生成Sql IN表达式)
        /// </summary>
        /// <typeparam name="TEntity">对象类型</typeparam>
        /// <typeparam name="TProperty">对象属性类型</typeparam>
        /// <param name="source">数据源</param>
        /// <param name="selector">指定对象某属性的表达式</param>
        /// <param name="selectorSource">子查询数据源</param>
        /// <param name="isIn">IN 或 NOT IN</param>
        /// <returns>新的数据源</returns>
        public static IDbQuery <TEntity> In <TEntity, TProperty>(this IDbQuery <TEntity> source, Expression <Func <TEntity, TProperty> > selector, IDbSelectedQuery <TProperty> selectorSource, bool isIn = true)
            where TEntity : class
        {
            //非空验证
            Check.ArgumentNull(source, nameof(source));
            Check.ArgumentNull(selector, nameof(selector));
            Check.ArgumentNull(selectorSource, nameof(selectorSource));
            //获取sql表达式模板
            MemberExpression memberExpression = selector.Body.GetMemberExpression();
            StringBuilder    sqlTemplate      = new StringBuilder();

            sqlTemplate.AppendLine(isIn ? "IN " : "NOT IN ");
            sqlTemplate.Append("\t(");
            sqlTemplate.Append(selectorSource.ToWhereSqlString().Trim());
            sqlTemplate.Append(")");
            sqlTemplate.Replace("\n ", "\n\t");
            //过滤获取新数据源
            return(source.Factory.CreateQuery(source, memberExpression, sqlTemplate.ToString(), selectorSource.Parameters.ToArray()));
        }
Exemplo n.º 2
0
 /// <summary>
 /// Extendable method: 获取左关联查询数据源
 /// </summary>
 /// <typeparam name="TEntity">实体类型</typeparam>
 /// <typeparam name="TOther">关联的实体类型</typeparam>
 /// <param name="source">数据源</param>
 /// <param name="otherSource">关联对象的选择性查询数据源</param>
 /// <param name="selector">指定关联实体类型的属性表达式</param>
 /// <param name="predicate">TEntity 与 TOther关系表达式</param>
 /// <returns>左关联查询数据源</returns>
 public static IDbQuery <TEntity> LeftJoin <TEntity, TOther>(this IDbQuery <TEntity> source, IDbSelectedQuery <TOther> otherSource, Expression <Func <TEntity, TOther> > selector, Expression <Func <TEntity, TOther, bool> > predicate)
     where TEntity : class
     where TOther : class
 {
     //非空检查
     Check.ArgumentNull(source, nameof(source));
     Check.ArgumentNull(otherSource, nameof(otherSource));
     Check.ArgumentNull(selector, nameof(selector));
     Check.ArgumentNull(predicate, nameof(predicate));
     //创建关联查询
     return(source.Factory.CreateLeftJoinedQuery(source, otherSource, selector, predicate));
 }