Esempio n. 1
0
        /// <summary>
        /// 创建关联一个强类型查询
        /// </summary>
        /// <typeparam name="TJoin"></typeparam>
        /// <typeparam name="TJoinResult"></typeparam>
        /// <param name="resultSelect"></param>
        /// <param name="expression"></param>
        /// <param name="joinType"></param>
        /// <returns></returns>
        public LambdaQueryViewJoin <T, TJoin, TJoinResult> Join <TJoin, TJoinResult>(LambdaQueryResultSelect <TJoin, TJoinResult> resultSelect, Expression <Func <T, TJoinResult, bool> > expression, JoinType joinType = JoinType.Inner)
        {
            if (!resultSelect.BaseQuery.__FromDbContext)
            {
                throw new CRLException("关联需要由LambdaQuery.CreateQuery创建");
            }
            var query2    = new LambdaQueryViewJoin <T, TJoin, TJoinResult>(this, resultSelect);
            var innerType = typeof(TJoin);
            //__JoinTypes.Add(new TypeQuery(innerType, "T_" + prefixIndex), joinType);
            var prefix1   = GetPrefix(innerType);
            var prefix2   = GetPrefix(typeof(TJoinResult));
            var typeQuery = new TypeQuery(innerType, prefix2);
            var baseQuery = resultSelect.BaseQuery;

            foreach (var kv in baseQuery.QueryParames)
            {
                QueryParames[kv.Key] = kv.Value;
            }
            string innerQuery = baseQuery.GetQuery();

            typeQuery.InnerQuery = innerQuery;
            string condition = FormatJoinExpression(expression.Body);

            AddInnerRelation(typeQuery, joinType, condition);
            return(query2);
        }
Esempio n. 2
0
        /// <summary>
        /// 创建关联一个强类型查询
        /// </summary>
        /// <typeparam name="TJoinResult"></typeparam>
        /// <param name="resultSelect"></param>
        /// <param name="expression"></param>
        /// <param name="joinType"></param>
        /// <returns></returns>
        public LambdaQueryViewJoin <T, TJoinResult> Join <TJoinResult>(LambdaQueryResultSelect <TJoinResult> resultSelect, Expression <Func <T, TJoinResult, bool> > expression, JoinType joinType = JoinType.Inner)
        {
            if (!resultSelect.BaseQuery.__FromDbContext)
            {
                throw new CRLException("关联需要由LambdaQuery.CreateQuery创建");
            }
            var query2 = new LambdaQueryViewJoin <T, TJoinResult>(this, resultSelect);
            //var innerType = typeof(TSource);
            var innerType = resultSelect.InnerType;

            var prefix1   = GetPrefix(innerType);
            var prefix2   = GetPrefix(typeof(TJoinResult));
            var typeQuery = new TypeQuery(innerType, prefix2);
            var baseQuery = resultSelect.BaseQuery;

            QueryParames.AddRange(baseQuery.QueryParames);

            string innerQuery = baseQuery.GetQuery();

            typeQuery.InnerQuery = innerQuery;
            string condition = FormatJoinExpression(expression.Body);

            AddInnerRelation(typeQuery, joinType, condition);
            return(query2);
        }
Esempio n. 3
0
 /// <summary>
 /// 联合查询
 /// 会清除父查询的排序
 /// </summary>
 /// <typeparam name="TResult2"></typeparam>
 /// <param name="resultSelect"></param>
 /// <param name="unionType"></param>
 /// <returns></returns>
 public LambdaQueryResultSelect <TResult> Union <TResult2>(LambdaQueryResultSelect <TResult2> resultSelect, UnionType unionType = UnionType.UnionAll)
 {
     BaseQuery.__QueryOrderBy = "";//清除OrderBy
     BaseQuery.AddUnion(resultSelect.BaseQuery, unionType);
     return(this);
 }
Esempio n. 4
0
 /// <summary>
 /// 关联查询分支
 /// </summary>
 /// <param name="query"></param>
 /// <param name="resultSelect"></param>
 internal LambdaQueryViewJoin(LambdaQuery <T> query, LambdaQueryResultSelect <TJoin, TJoinResult> resultSelect)
 {
     BaseQuery = query;
 }
Esempio n. 5
0
        LambdaQuery <T> InnerSelect <TResult>(Expression <Func <T, TResult> > outField, LambdaQueryResultSelect <TResult> query, string type, string innerJoinSql = "")
        {
            if (!query.BaseQuery.__FromDbContext)
            {
                throw new CRLException("关联需要由LambdaQuery.CreateQuery创建");
            }
            var baseQuery = query.BaseQuery;

            foreach (var kv in baseQuery.QueryParames)
            {
                QueryParames[kv.Key] = kv.Value;
            }
            var query2 = baseQuery.GetQuery();

            return(InnerSelect(outField, query2, type));
        }
Esempio n. 6
0
 /// <summary>
 /// 按!=
 /// 等效为table.field !=(select field from table2)
 /// </summary>
 /// <typeparam name="TResult"></typeparam>
 /// <param name="query"></param>
 /// <param name="outField"></param>
 /// <returns></returns>
 public LambdaQuery <T> NotEqual <TResult>(LambdaQueryResultSelect <TResult> query, Expression <Func <T, TResult> > outField)
 {
     return(InnerSelect(outField, query, "!="));
 }
Esempio n. 7
0
 /// <summary>
 /// 按查询not exists
 /// 等效为 not exixts(select field from table2)
 /// </summary>
 /// <typeparam name="TResult"></typeparam>
 /// <param name="query"></param>
 /// <returns></returns>
 public LambdaQuery <T> NotExists <TResult>(LambdaQueryResultSelect <TResult> query)
 {
     return(InnerSelect(null, query, "not exists"));
 }
Esempio n. 8
0
        protected LambdaQuery <T> InnerSelect <T2, TResult>(Expression <Func <T, TResult> > outField, LambdaQueryResultSelect <T2, TResult> query, string type, Expression <Func <T, T2, bool> > expression = null)
        {
            if (!query.BaseQuery.__FromDbContext)
            {
                throw new CRLException("关联需要由LambdaQuery.CreateQuery创建");
            }
            MemberExpression m1 = null;

            //object 会生成UnaryExpression表达式 Convert(b=>b.UserId)
            if (outField != null)//兼容exists 可能为空
            {
                if (outField.Body is UnaryExpression)
                {
                    m1 = (outField.Body as UnaryExpression).Operand as MemberExpression;
                }
                else
                {
                    m1 = outField.Body as MemberExpression;
                }
            }
            string field1 = "";

            if (outField != null)
            {
                field1 = string.Format("{0}{1}", GetPrefix(), __DBAdapter.KeyWordFormat(m1.Member.Name));
            }
            string condition = "";
            var    query2    = query.BaseQuery.GetQuery();

            if (expression != null)
            {
                //内部关联
                GetPrefix(typeof(T2));
                string condition2 = FormatJoinExpression(expression.Body);
                query2 += " and " + condition2;
            }
            condition = string.Format("{0} {1}({2})", field1, type, query2);
            if (Condition.Length > 0)
            {
                condition = " and " + condition;
            }
            Condition.Append(condition);
            return(this);
        }
Esempio n. 9
0
 /// <summary>
 /// 按查询not in
 /// 等效为table.field not in(select field from table2)
 /// </summary>
 /// <typeparam name="T2"></typeparam>
 /// <typeparam name="TResult"></typeparam>
 /// <param name="query"></param>
 /// <param name="outField"></param>
 /// <returns></returns>
 public LambdaQuery <T> NotIn <T2, TResult>(LambdaQueryResultSelect <T2, TResult> query, Expression <Func <T, TResult> > outField)
 {
     return(InnerSelect(outField, query, "not in"));
 }
Esempio n. 10
0
 /// <summary>
 /// 按查询in
 /// 等效为table.field in(select field from table2)
 /// </summary>
 /// <typeparam name="T2"></typeparam>
 /// <typeparam name="TResult"></typeparam>
 /// <param name="query"></param>
 /// <param name="outField"></param>
 /// <param name="expression">内关联</param>
 /// <returns></returns>
 public LambdaQuery <T> In <T2, TResult>(LambdaQueryResultSelect <T2, TResult> query, Expression <Func <T, TResult> > outField, Expression <Func <T, T2, bool> > expression = null)
 {
     return(InnerSelect(outField, query, "in", expression));
 }