예제 #1
0
        /// <summary>
        /// Filters the elements of <paramref name="source"/> based on <paramref name="targetResourceType"/>
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="targetResourceType">targetResourceType</param>
        /// <returns>An expression for IEnumerable(Of T) that contains elements from the source sequence of type <paramref name="targetResourceType"/>.</returns>
        internal static Expression GenerateOfType(Expression source, ResourceType targetResourceType)
        {
            if (targetResourceType.CanReflectOnInstanceType)
            {
                return(source.EnumerableOfType(targetResourceType.InstanceType));
            }

            source = Expression.Call(
                DataServiceProviderMethods.OfTypeIEnumerableMethodInfo.MakeGenericMethod(
                    BaseServiceProvider.GetIEnumerableElement(source.Type),
                    targetResourceType.InstanceType),
                source,
                Expression.Constant(targetResourceType));

            return(source.EnumerableCast(targetResourceType.InstanceType));
        }
예제 #2
0
        /// <summary>
        /// Compose SelectMany() to expression
        /// </summary>
        /// <param name="genericMethodInfo">SelectMany MethodInfo</param>
        /// <param name="source">Source expression</param>
        /// <param name="selector">Selector expression</param>
        /// <returns>Expression with SelectMany()</returns>
        private static Expression SelectMany(MethodInfo genericMethodInfo, Expression source, LambdaExpression selector)
        {
            Debug.Assert(genericMethodInfo != null && genericMethodInfo.IsGenericMethod, "genericMethodInfo != null && genericMethodInfo.IsGenericMethod");
            Debug.Assert(source != null, "source != null");
            Debug.Assert(selector != null, "selector != null");

            Type elementType = source.ElementType();

            Debug.Assert(elementType != null, "elementType != null");

            Type       resultElementType = BaseServiceProvider.GetIEnumerableElement(selector.Body.Type);
            MethodInfo selectManyMethod  = genericMethodInfo.MakeGenericMethod(elementType, resultElementType);

            // Note the ParameterType on an IQueryable mehtod is Expression<Func<>> where as the ParameterType
            // on an IEnumerable method is Func<>.
            Debug.Assert(
                selectManyMethod.GetParameters()[1].ParameterType == selector.GetType() ||
                selectManyMethod.GetParameters()[1].ParameterType == selector.Type,
                "selector should be of type Expression<Func<TSource, IEnumerable<TResult>>>");
            return(Expression.Call(null, selectManyMethod, source, selector));
        }
예제 #3
0
 /// <summary>
 /// Returns the element type of the expression.
 /// </summary>
 /// <param name="source">Source expression.</param>
 /// <returns>Returns the element type of the expression.</returns>
 internal static Type ElementType(this Expression source)
 {
     Debug.Assert(source != null, "source != null");
     return(BaseServiceProvider.GetIEnumerableElement(source.Type) ?? source.Type);
 }