public override IQueryState Visit(JoinQueryExpression exp) { ResultElement resultElement = new ResultElement(); IQueryState qs = QueryExpressionVisitor.VisitQueryExpression(exp.PrevExpression); FromQueryResult fromQueryResult = qs.ToFromQueryResult(); DbFromTableExpression fromTable = fromQueryResult.FromTable; resultElement.FromTable = fromTable; List <IMappingObjectExpression> moeList = new List <IMappingObjectExpression>(); moeList.Add(fromQueryResult.MappingObjectExpression); foreach (JoiningQueryInfo joiningQueryInfo in exp.JoinedQueries) { JoinQueryResult joinQueryResult = JoinQueryExpressionVisitor.VisitQueryExpression(joiningQueryInfo.Query.QueryExpression, resultElement, joiningQueryInfo.JoinType, joiningQueryInfo.Condition, moeList); var nullChecking = DbExpression.CaseWhen(new DbCaseWhenExpression.WhenThenExpressionPair(joinQueryResult.JoinTable.Condition, DbConstantExpression.One), DbConstantExpression.Null, DbConstantExpression.One.Type); if (joiningQueryInfo.JoinType == JoinType.LeftJoin) { joinQueryResult.MappingObjectExpression.SetNullChecking(nullChecking); } else if (joiningQueryInfo.JoinType == JoinType.RightJoin) { foreach (IMappingObjectExpression item in moeList) { item.SetNullChecking(nullChecking); } } else if (joiningQueryInfo.JoinType == JoinType.FullJoin) { joinQueryResult.MappingObjectExpression.SetNullChecking(nullChecking); foreach (IMappingObjectExpression item in moeList) { item.SetNullChecking(nullChecking); } } fromTable.JoinTables.Add(joinQueryResult.JoinTable); moeList.Add(joinQueryResult.MappingObjectExpression); } IMappingObjectExpression moe = SelectorExpressionVisitor.ResolveSelectorExpression(exp.Selector, moeList); resultElement.MappingObjectExpression = moe; GeneralQueryState queryState = new GeneralQueryState(resultElement); return(queryState); }
public IQuery <TResult> Select <TResult>(Expression <Func <T1, T2, TResult> > selector) { if (this._filterPredicates.Count == 0) { JoinQueryExpression e1 = new JoinQueryExpression(typeof(TResult), this._rootQuery.QueryExpression, this._joinedQueries, selector); return(new Query <TResult>(this.DbContext, e1, this._rootQuery.TrackEntity)); } /* * q.LeftJoin<City>((user, city) => user.CityId == city.Id).Where((user, city) => user.Name == "lu").Select((user, city) => new { user, city }) * 转换成: * q.LeftJoin<City>((user, city) => user.CityId == city.Id) * .Select((user, city) => new JoinResult<T1, T2>() { Item1 = user, Item2 = city }) * .Where(a => a.Item1.Name == "lu") * .Select(a => new { user = a.user, city = a.city }) */ Type joinResultType = typeof(JoinResult <T1, T2>); Expression <Func <T1, T2, JoinResult <T1, T2> > > joinResultSelector = (t1, t2) => new JoinResult <T1, T2>() { Item1 = t1, Item2 = t2 }; JoinQueryExpression e = new JoinQueryExpression(joinResultType, this._rootQuery.QueryExpression, this._joinedQueries, joinResultSelector); IQuery <JoinResult <T1, T2> > q = new Query <JoinResult <T1, T2> >(this.DbContext, e, this._rootQuery.TrackEntity); ParameterExpression parameter = Expression.Parameter(joinResultType, "a"); Expression[] expressionSubstitutes = QueryHelper.MakeExpressionSubstitutes(joinResultType, parameter); Expression <Func <JoinResult <T1, T2>, bool> > predicate = QueryHelper.ComposePredicate <Func <JoinResult <T1, T2>, bool> >(this._filterPredicates, expressionSubstitutes, parameter); var q1 = q.Where(predicate); Expression <Func <JoinResult <T1, T2>, TResult> > selector2 = JoinQueryParameterExpressionReplacer.Replace(selector, expressionSubstitutes, parameter) as Expression <Func <JoinResult <T1, T2>, TResult> >; var ret = q1.Select(selector2); return(ret); }
public virtual void WriteJoinQueryParameter( JoinQueryExpression joinQueryExpression, QueryExpressionVisitor queryExpressionVisitor ) { switch (joinQueryExpression.Direction) { case JoinDirection.Inner: queryText.Append(" INNER "); break; case JoinDirection.Left: queryText.Append(" LEFT OUTER "); break; case JoinDirection.Right: queryText.Append(" RIGHT OUTER "); break; } queryText.Append(" JOIN "); WriteIfNotNull(joinQueryExpression.TargetExpression, queryExpressionVisitor); queryText.Append(" ON "); WriteIfNotNull(joinQueryExpression.OnExpression, queryExpressionVisitor); }
public override JoinQueryResult Visit(JoinQueryExpression exp) { JoinQueryResult ret = this.Visit(exp); return(ret); }
public override IQueryState Visit(JoinQueryExpression exp) { IQueryState qs = QueryExpressionVisitor.VisitQueryExpression(exp.PrevExpression, this._scopeParameters, this._scopeTables); ResultElement resultElement = qs.ToFromQueryResult(); List <IMappingObjectExpression> moeList = new List <IMappingObjectExpression>(); moeList.Add(resultElement.MappingObjectExpression); foreach (JoiningQueryInfo joiningQueryInfo in exp.JoinedQueries) { ScopeParameterDictionary scopeParameters = resultElement.ScopeParameters.Clone(resultElement.ScopeParameters.Count + moeList.Count); for (int i = 0; i < moeList.Count; i++) { ParameterExpression p = joiningQueryInfo.Condition.Parameters[i]; scopeParameters[p] = moeList[i]; } JoinQueryResult joinQueryResult = JoinQueryExpressionVisitor.VisitQueryExpression(joiningQueryInfo.Query.QueryExpression, resultElement, joiningQueryInfo.JoinType, joiningQueryInfo.Condition, scopeParameters); var nullChecking = DbExpression.CaseWhen(new DbCaseWhenExpression.WhenThenExpressionPair(joinQueryResult.JoinTable.Condition, DbConstantExpression.One), DbConstantExpression.Null, DbConstantExpression.One.Type); if (joiningQueryInfo.JoinType == JoinType.LeftJoin) { joinQueryResult.MappingObjectExpression.SetNullChecking(nullChecking); } else if (joiningQueryInfo.JoinType == JoinType.RightJoin) { foreach (IMappingObjectExpression item in moeList) { item.SetNullChecking(nullChecking); } } else if (joiningQueryInfo.JoinType == JoinType.FullJoin) { joinQueryResult.MappingObjectExpression.SetNullChecking(nullChecking); foreach (IMappingObjectExpression item in moeList) { item.SetNullChecking(nullChecking); } } resultElement.FromTable.JoinTables.Add(joinQueryResult.JoinTable); moeList.Add(joinQueryResult.MappingObjectExpression); } ScopeParameterDictionary scopeParameters1 = resultElement.ScopeParameters.Clone(resultElement.ScopeParameters.Count + moeList.Count); for (int i = 0; i < moeList.Count; i++) { ParameterExpression p = exp.Selector.Parameters[i]; scopeParameters1[p] = moeList[i]; } IMappingObjectExpression moe = SelectorExpressionVisitor.ResolveSelectorExpression(exp.Selector, scopeParameters1, resultElement.ScopeTables); resultElement.MappingObjectExpression = moe; GeneralQueryState queryState = new GeneralQueryState(resultElement); return(queryState); }
public IQuery <TResult> Select <TResult>(Expression <Func <T1, T2, TResult> > selector) { JoinQueryExpression e = new JoinQueryExpression(typeof(TResult), this._rootQuery.QueryExpression, this._joinedQueries, selector); return(new Query <TResult>(this.DbContext, e, this._rootQuery.TrackEntity)); }
public override IQueryState Visit(JoinQueryExpression exp) { IQueryState qs = QueryExpressionResolver.Resolve(exp.PrevExpression, this._scopeParameters, this._scopeTables); QueryModel queryModel = qs.ToFromQueryModel(); List <IObjectModel> modelList = new List <IObjectModel>(); modelList.Add(queryModel.ResultModel); foreach (JoinQueryInfo joinQueryInfo in exp.JoinedQueries) { ScopeParameterDictionary scopeParameters = queryModel.ScopeParameters.Clone(queryModel.ScopeParameters.Count + modelList.Count); for (int i = 0; i < modelList.Count; i++) { ParameterExpression p = joinQueryInfo.Condition.Parameters[i]; scopeParameters[p] = modelList[i]; } JoinQueryResult joinQueryResult = JoinQueryExpressionResolver.Resolve(joinQueryInfo, queryModel, scopeParameters); var nullChecking = DbExpression.CaseWhen(new DbCaseWhenExpression.WhenThenExpressionPair(joinQueryResult.JoinTable.Condition, DbConstantExpression.One), DbConstantExpression.Null, DbConstantExpression.One.Type); if (joinQueryInfo.JoinType == JoinType.LeftJoin) { joinQueryResult.ResultModel.SetNullChecking(nullChecking); } else if (joinQueryInfo.JoinType == JoinType.RightJoin) { foreach (IObjectModel item in modelList) { item.SetNullChecking(nullChecking); } } else if (joinQueryInfo.JoinType == JoinType.FullJoin) { joinQueryResult.ResultModel.SetNullChecking(nullChecking); foreach (IObjectModel item in modelList) { item.SetNullChecking(nullChecking); } } queryModel.FromTable.JoinTables.Add(joinQueryResult.JoinTable); modelList.Add(joinQueryResult.ResultModel); } ScopeParameterDictionary scopeParameters1 = queryModel.ScopeParameters.Clone(queryModel.ScopeParameters.Count + modelList.Count); for (int i = 0; i < modelList.Count; i++) { ParameterExpression p = exp.Selector.Parameters[i]; scopeParameters1[p] = modelList[i]; } IObjectModel model = SelectorResolver.Resolve(exp.Selector, scopeParameters1, queryModel.ScopeTables); queryModel.ResultModel = model; GeneralQueryState queryState = new GeneralQueryState(queryModel); return(queryState); }