protected override Expression VisitMember(MemberExpression m) { var source = Visit(m.Expression); IProperty property; // **fix** 解决无法返回两级以上关联对象的问题 // var ex = source as EntityExpression // if (ex != null && if ((property = PropertyUnity.GetProperty(m.Expression.Type, m.Member.Name)) != null && property is RelationProperty) { var projection = (ProjectionExpression)Visit(QueryUtility.GetMemberExpression(source, property)); if (currentFrom != null && m.Member.GetMemberType().GetEnumerableType() == null) { // convert singleton associations directly to OUTER APPLY projection = projection.AddOuterJoinTest(); var newFrom = new JoinExpression(JoinType.OuterApply, currentFrom, projection.Select, null); currentFrom = newFrom; return(projection.Projector); } return(projection); } var result = QueryBinder.BindMember(source, m.Member); if (result is MemberExpression mex && mex.Member == m.Member && mex.Expression == m.Expression) { return(m); } return(result); }
protected override Expression VisitEntity(EntityExpression entity) { var init = entity.Expression as MemberInitExpression; if (init != null) { var save = includeScope; includeScope = new ScopedDictionary <IProperty, bool>(this.includeScope); List <MemberBinding> newBindings = null; foreach (var property in PropertyUnity.GetRelatedProperties(entity.Type)) { PropertyInfo member; if (property is IPropertyLazy && policy.IsIncluded(member = property.Info.ReflectionInfo)) { if (includeScope.ContainsKey(property)) { throw new NotSupportedException(string.Format("Cannot include '{0}.{1}' recursively.", property.Type, property.Name)); } var me = QueryUtility.GetMemberExpression(init, property); if (newBindings == null) { newBindings = new List <MemberBinding>(init.Bindings); } newBindings.Add(Expression.Bind(member, me)); } } if (newBindings != null) { entity = new EntityExpression(entity.Metadata, Expression.MemberInit(init.NewExpression, newBindings)); } includeScope = save; } return(base.VisitEntity(entity)); }