protected override Expression VisitMethodCall(MethodCallExpression methodExp) { if (methodExp.Method.Name == "OrderByDescending" || methodExp.Method.Name == "ThenByDescending") { OrderByFields.Clear(); OrderBy = "DESC"; Expression obj = this.Visit(((LambdaExpression)methodExp.Arguments[1]).Body); OrderByFieldsStack.Insert(0, OrderByFields.ToString().TrimEnd(',')); OrderByFields.Clear(); if (methodExp.Arguments[0].NodeType == ExpressionType.Call) { this.Visit(methodExp.Arguments[0]); } return(methodExp); } else if (methodExp.Method.Name == "OrderBy" || methodExp.Method.Name == "ThenBy") { OrderByFields.Clear(); OrderBy = "ASC"; Expression obj = this.Visit(((LambdaExpression)methodExp.Arguments[1]).Body); OrderByFieldsStack.Insert(0, OrderByFields.ToString().TrimEnd(',')); OrderByFields.Clear(); if (methodExp.Arguments[0].NodeType == ExpressionType.Call) { this.Visit(methodExp.Arguments[0]); } return(methodExp); } else { Expression obj = this.Visit(methodExp.Object); object result = Expression.Lambda(methodExp).Compile().DynamicInvoke(); var r = Expression.Constant(result, methodExp.Method.ReturnType); this.Visit(r); return(r); } throw new NotSupportedException(string.Format("lambda表达式不支持使用此'{0}'方法", methodExp.Method.Name)); }
/// <summary> /// 多表连查分页 /// </summary> /// <typeparam name="TModel"></typeparam> /// <param name="page"></param> /// <returns></returns> public PageDataSource <TModel> GetPage <TModel>(PageFilter page) { List <JoinRelation> tables = new List <JoinRelation>(); SqlBuilder.AppendFormat(" SELECT {0} FROM {1}", SelectFields.ToString().TrimEnd(','), JoinRelations[0].LeftTable); if (WithAlias) { SqlBuilder.AppendFormat(" AS {0}", JoinRelations[0].LeftTableAlias); } foreach (JoinRelation j in JoinRelations) { if (j.JoinType == JoinType.Outer) { SqlBuilder.Append(" LEFT OUTER JOIN "); } else { SqlBuilder.Append(" INNER JOIN "); } if (WithAlias) { if (tables.Count(m => m.LeftTableAlias == j.RightTableAlias || m.RightTableAlias == j.RightTableAlias) == 0) { SqlBuilder.AppendFormat("{0} AS {1}", j.RightTable, j.RightTableAlias); tables.Add(j); } else { SqlBuilder.AppendFormat("{0} AS {1}", j.LeftTable, j.LeftTableAlias); tables.Add(j); } } else { if (tables.Count(m => m.LeftTable == j.RightTable || m.RightTable == j.RightTable) == 0) { SqlBuilder.Append(j.RightTable); tables.Add(j); } else { SqlBuilder.Append(j.LeftTable); tables.Add(j); } } SqlBuilder.AppendFormat(" ON {0}", j.OnSql.TrimEnd("AND".ToCharArray())); } if (WhereClause.Length > 0) { SqlBuilder.AppendFormat(" WHERE {0}", WhereClause.ToString().Trim().TrimEnd("AND".ToCharArray())); } if (GroupByFields.Length > 0) { SqlBuilder.AppendFormat(" GROUP BY {0}", GroupByFields.ToString().Trim().TrimEnd(',')); } if (OrderByFields.Length > 0) { page.OrderText = OrderByFields.ToString().Trim().TrimEnd(','); } //开始组装sql //开始组装sql PageDataSource <TModel> result = DbContext.GetPage <TModel>(SqlBuilder.ToString(), page, Parameters.ToArray()); if (this.IsAutoDisposeDbContext) { DbContext.Dispose(); } return(result); }
/// <summary> /// 返回数量 /// </summary> /// <returns></returns> public int GetCount() { List <JoinRelation> tables = new List <JoinRelation>(); SqlBuilder.AppendFormat(" SELECT COUNT(1) AS CT FROM {0}", JoinRelations[0].LeftTable); if (WithAlias) { SqlBuilder.AppendFormat(" AS {0}", JoinRelations[0].LeftTableAlias); } foreach (JoinRelation j in JoinRelations) { if (j.JoinType == JoinType.Outer) { SqlBuilder.Append(" LEFT OUTER JOIN "); } else { SqlBuilder.Append(" INNER JOIN "); } if (WithAlias) { if (tables.Count(m => m.LeftTableAlias == j.RightTableAlias || m.RightTableAlias == j.RightTableAlias) == 0) { SqlBuilder.AppendFormat("{0} AS {1}", j.RightTable, j.RightTableAlias); tables.Add(j); } else { SqlBuilder.AppendFormat("{0} AS {1}", j.LeftTable, j.LeftTableAlias); tables.Add(j); } } else { if (tables.Count(m => m.LeftTable == j.RightTable || m.RightTable == j.RightTable) == 0) { SqlBuilder.Append(j.RightTable); tables.Add(j); } else { SqlBuilder.Append(j.LeftTable); tables.Add(j); } } SqlBuilder.AppendFormat(" ON {0}", j.OnSql.TrimEnd("AND".ToCharArray())); } if (WhereClause.Length > 0) { SqlBuilder.AppendFormat(" WHERE {0}", WhereClause.ToString().Trim().TrimEnd("AND".ToCharArray())); } if (GroupByFields.Length > 0) { SqlBuilder.AppendFormat(" GROUP BY {0}", GroupByFields.ToString().Trim().TrimEnd(',')); } if (OrderByFields.Length > 0) { SqlBuilder.AppendFormat(" ORDER BY {0}", OrderByFields.ToString().Trim().TrimEnd(',')); } //开始组装sql int result = DbContext.GetSingle <int>(SqlBuilder.ToString(), Parameters.ToArray()); if (this.IsAutoDisposeDbContext) { DbContext.Dispose(); } return(result); }