/// <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); }
/// <summary> /// 创建关联一个强类型查询 /// </summary> /// <typeparam name="TJoinResult"></typeparam> /// <param name="resultSelect"></param> /// <param name="expression"></param> /// <param name="joinType"></param> /// <returns></returns> public ILambdaQueryViewJoin <T, TJoinResult> Join <TJoinResult>(ILambdaQueryResultSelect <TJoinResult> resultSelect, Expression <Func <T, TJoinResult, bool> > expression, JoinType joinType = JoinType.Inner) { if (!resultSelect.BaseQuery.__FromDbContext) { throw new Exception("关联需要由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); }
/// <summary> /// 在Join后增加条件 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="expressionBody"></param> internal void __JoinAfter <T>(Expression expressionBody) { string condition = FormatExpression(expressionBody).SqlOut; condition = " and " + condition; var innerType = typeof(T); var typeQuery = new TypeQuery(innerType); __Relations[typeQuery].condition += condition; }
/// <summary> /// 在Join后增加条件 /// </summary> /// <param name="expression"></param> /// <returns></returns> public LambdaQueryJoin <T, TJoin> JoinAfter(Expression <Func <TJoin, bool> > expression) { string condition = BaseQuery.FormatExpression(expression.Body).SqlOut; condition = " and " + condition; var innerType = typeof(TJoin); var typeQuery = new TypeQuery(innerType); BaseQuery.__Relations[typeQuery].condition += condition; return(this); }
//internal void AddInnerRelationCondition(TypeQuery inner, string condition) //{ // __Relations[inner] += " and " + condition; //} internal void AddInnerRelation(TypeQuery typeQuery, JoinType joinType, string condition) { if (__Relations == null) { __Relations = new Dictionary <TypeQuery, JoinInfo>(); } var inner = typeQuery.OriginType; if (__Relations.ContainsKey(typeQuery)) { throw new CRLException(string.Format("关联查询已包含关联对象 {0} {1}", inner, condition)); return; } //if (__MainType == inner) //{ // throw new CRLException(string.Format("关联查询不能指定自已 {0} {1}" , inner,condition)); // return; //} if (inner.IsSubclassOf(typeof(IModel))) { DBExtendFactory.CreateDBExtend(__DbContext).CheckTableCreated(inner); } var tableName = TypeCache.GetTableName(inner, __DbContext); string aliasName = GetPrefix(inner); aliasName = aliasName.Substring(0, aliasName.Length - 1); if (typeQuery.TypeQueryEnum == TypeQueryEnum.查询) { //查询别名按关联别名算 condition = condition.Replace(typeQuery.queryName2, aliasName + "."); tableName = string.Format("({0}) {1} ", typeQuery.InnerQuery, aliasName); } else { tableName = string.Format("{0} {1} {2}", __DBAdapter.KeyWordFormat(tableName), aliasName, __DBAdapter.GetWithNolockFormat(__WithNoLock)); } //string str = string.Format(" {0} join {1} on {2}", joinType, tableName, condition); if (!__Relations.ContainsKey(typeQuery)) { var join = new JoinInfo() { joinType = joinType, tableName = tableName, condition = condition }; __Relations.Add(typeQuery, join); } }
internal void AddInnerRelationCondition(TypeQuery inner, string condition) { __Relations[inner] += " and " + condition; }