Esempio n. 1
0
        private MemberBinding ToMemberBinding(Expression it, SelectClause selectClause)
        {
            Expression navigationProperty = Expression.MakeMemberAccess(it, _navigationProperty);

            if (_isCollection)
            {
                // NavigationProperty = it.NavigationProperty.Select( arg => new Item { ... } ).ToArray()
                // NavigationProperty = it.NavigationProperty.Select( arg => new Item { ... } ).ToList()

                // apply all constraints
                if (Select != null)
                {
                    navigationProperty = Select.ApplyTo(navigationProperty);
                }
                if (Orderby != null)
                {
                    navigationProperty = Orderby.ApplyTo(navigationProperty);
                }
                if (Filter != null)
                {
                    navigationProperty = Filter.ApplyTo(navigationProperty);
                }
                if (Skip != null)
                {
                    navigationProperty = Skip.ApplyTo(navigationProperty, ItemType);
                }
                if (Top != null)
                {
                    navigationProperty = Top.ApplyTo(navigationProperty, ItemType);
                }

                // build proper select of properties
                var expression = BuildSelectLambdaExpression(ItemType, selectClause, NestedExpands);

                navigationProperty = Expression.Call(null,
                                                     SelectInfo.MakeGenericMethod(ItemType, ItemType),
                                                     navigationProperty,
                                                     expression
                                                     );

                // add ToList/ToArray
                var loadFunction = _navigationProperty.PropertyType.IsArray
                    ? ToArrayInfo.MakeGenericMethod(ItemType)
                    : ToListInfo.MakeGenericMethod(ItemType);

                navigationProperty = Expression.Call(null,
                                                     loadFunction,
                                                     navigationProperty
                                                     );
            }
            else
            {
                // NavigationProperty = new Item { ... }
                navigationProperty = BuildMemberInit(ItemType, navigationProperty, selectClause, NestedExpands);
            }

            return(Expression.Bind(_navigationProperty, navigationProperty));
        }