/// <summary>
        /// Creates an ObjectQuery from an expression.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="expression">The expression.</param>
        /// <param name="type">The type.</param>
        /// <returns>An ObjectQuery created from the expression.</returns>
        public static ObjectQuery CreateQuery(this IQueryable source, Expression expression, Type type)
        {
            // first convert to object query to get the correct provider
            ObjectQuery sourceQuery = source.ToObjectQuery();

            if (sourceQuery == null)
            {
                return(null);
            }

            IQueryProvider provider = ((IQueryable)sourceQuery).Provider;

            // create query from expression using internal ObjectQueryProvider
            dynamic    providerProxy   = new DynamicProxy(provider);
            IQueryable expressionQuery = providerProxy.CreateQuery(expression, type);

            if (expressionQuery == null)
            {
                return(null);
            }

            // convert back to ObjectQuery
            ObjectQuery resultQuery = expressionQuery.ToObjectQuery();

            return(resultQuery);
        }