// 选择所有的字段 private Expression VisitAllMember(Type type, string alias, Expression node = null) { TypeRuntimeInfo runtimeInfo = TypeRuntimeInfoCache.GetRuntimeInfo(type); Dictionary <string, Inte.XFramework.Reflection.MemberAccessWrapper> wrappers = runtimeInfo.Wrappers; //Fixed issue# 匿名类的字段不能Set //runtimeInfo.IsAnonymousType //? type.GetProperties().ToDictionary(p => p.Name, p => new Inte.XFramework.Reflection.MemberAccessWrapper(p)) //: runtimeInfo.Wrappers; foreach (var w in wrappers) { var wrapper = w.Value;// as Inte.XFramework.Reflection.MemberAccessWrapper; var mapper = wrapper as MemberAccessWrapper; if (mapper != null && mapper.Column != null && mapper.Column.NoMapped) { continue; } if (mapper != null && mapper.ForeignKey != null) { continue; // 不加载导航属性 } _builder.AppendMember(alias, wrapper.Member.Name); // 选择字段 string newName = ColumnExpressionVisitor.AddColumn(_columns, wrapper.Member.Name); // 添加字段别名 _builder.AppendAs(newName); _builder.Append(","); _builder.AppendNewLine(); } return(node); }
// g.Key.CompanyName & g.Max(a) protected override Expression VisitMember(MemberExpression node) { if (node == null) { return(node); } // Group By 解析 if (_groupBy != null && node.IsGrouping()) { // CompanyName = g.Key.Name LambdaExpression keySelector = _groupBy.Expressions[0] as LambdaExpression; Expression exp = null; Expression body = keySelector.Body; if (body.NodeType == ExpressionType.MemberAccess) { // group xx by a.CompanyName exp = body; // // // // } else if (body.NodeType == ExpressionType.New) { // group xx by new { Name = a.CompanyName } string memberName = node.Member.Name; NewExpression newExp = body as NewExpression; int index = newExp.Members.IndexOf(x => x.Name == memberName); exp = newExp.Arguments[index]; } return(this.Visit(exp)); } // 分组后再分页,子查询不能有OrderBy子句,此时要将OrderBy字段添加到选择字段中,抛给外层查询使用 var newNode = base.VisitMember(node); if (_mOnly) { ColumnExpressionVisitor.AddColumn(_columns, node.Member.Name); _builder.AppendAs(node.Member.Name); } return(newNode); }
//遍历New表达式的参数集 private Expression VisitAllArguments(NewExpression node) { for (int i = 0; i < node.Arguments.Count; i++) { Expression exp = node.Arguments[i]; Type pType = exp.Type; if (exp.NodeType == ExpressionType.Parameter) { // new Client(a) Type type = exp.Type; string alias = _aliases.GetTableAlias(exp); this.VisitAllMember(type, alias); continue; } if (exp.NodeType == ExpressionType.MemberAccess || exp.NodeType == ExpressionType.Call) { if (TypeUtils.IsPrimitive(pType)) { // new Client(a.ClientId) this.Visit(exp); // 选择字段 string newName = ColumnExpressionVisitor.AddColumn(_columns, node.Members != null ? node.Members[i].Name : (exp as MemberExpression).Member.Name); // 添加字段别名 _builder.AppendAs(newName); _builder.Append(','); _builder.AppendNewLine(); } else { this.VisitMember_Navigation(exp as MemberExpression); } continue; } throw new XfwException("NodeType '{0}' not supported.", exp.NodeType); } return(node); }
//{new App() {Id = p.Id}} protected override Expression VisitMemberInit(MemberInitExpression node) { if (node.NewExpression != null) { this.VisitAllArguments(node.NewExpression); } if (_navDescriptorKeys.Count == 0) { _navDescriptorKeys.Add(node.Type.Name); } //todo #对 Bindings 进行排序,保证导航属性的赋值一定要最后面# // 未实现,在书写表达式时人工保证 ## for (int i = 0; i < node.Bindings.Count; i++) { MemberAssignment binding = node.Bindings[i] as MemberAssignment; if (binding == null) { throw new XfwException("Only 'MemberAssignment' binding supported."); } Type pType = (node.Bindings[i].Member as System.Reflection.PropertyInfo).PropertyType; bool visitNav = !TypeUtils.IsPrimitive(pType); #region 一般属性 // 非导航属性 if (!visitNav) { base.VisitMemberBinding(node.Bindings[i]); // 选择字段 string newName = ColumnExpressionVisitor.AddColumn(_columns, binding.Member.Name); // 添加字段别名 _builder.AppendAs(newName); _builder.Append(','); _builder.AppendNewLine(); continue; } #endregion #region 导航属性 int n = _navDescriptorKeys.Count; string keyName = _navDescriptorKeys.Count > 0 ? _navDescriptorKeys[_navDescriptorKeys.Count - 1] : string.Empty; keyName = !string.IsNullOrEmpty(keyName) ? keyName + "." + binding.Member.Name : binding.Member.Name; ColumnNavDescriptor descriptor = new ColumnNavDescriptor(keyName, binding.Member); if (!_navDescriptors.ContainsKey(keyName)) { descriptor.Start = _columns.Count; descriptor.Count = CountField(binding.Expression); _navDescriptors.Add(keyName, descriptor); _navDescriptorKeys.Add(keyName); } if (binding.Expression.NodeType == ExpressionType.MemberAccess) { this.VisitMember_Navigation(binding.Expression as MemberExpression); } else if (binding.Expression.NodeType == ExpressionType.New) { this.VisitAllArguments(binding.Expression as NewExpression); } else if (binding.Expression.NodeType == ExpressionType.MemberInit) { this.VisitMemberInit(binding.Expression as MemberInitExpression); } if (_navDescriptorKeys.Count != n) { _navDescriptorKeys.RemoveAt(_navDescriptorKeys.Count - 1); } #endregion } return(node); }