private async Task <List <TResult> > ToListAsync(IQueryable <TResult> query)
        {
            IQueryable <object> runtimeTypeSelectExpressionQuery = ToObjectQuery(query);

            // Get result from database
            List <object> listOfObjects = await runtimeTypeSelectExpressionQuery.ToListAsync();

            MethodInfo castMethod = typeof(Queryable)
                                    .GetMethod("Cast", BindingFlags.Public | BindingFlags.Static)
                                    .MakeGenericMethod(_proxyType);

            // Cast list<objects> to IQueryable<runtimeType>
            IQueryable castedSource = castMethod.Invoke(
                null,
                new Object[] { listOfObjects.AsQueryable() }
                ) as IQueryable;

            // Create instance of runtime type parameter
            ParameterExpression runtimeParameter = Expression.Parameter(_proxyType, "p");

            IDictionary <string, PropertyInfo> dynamicTypeFieldsDict =
                _proxyProperties.ToDictionary(f => f.Name.ToLowerInvariant(), f => f);

            // Generate bindings from runtime type to source type
            IEnumerable <MemberBinding> bindingsToTargetType = _selectedProperties.Values
                                                               .Select(property => Expression.Bind(
                                                                           property,
                                                                           Expression.Property(
                                                                               runtimeParameter,
                                                                               dynamicTypeFieldsDict[property.Name.ToLowerInvariant()]
                                                                               )
                                                                           ));

            // Generate projection trom runtimeType to T and cast as IQueryable<object>
            IQueryable <TResult> targetTypeSelectExpressionQuery
                = ExpressionMapper.GenerateProjectedQuery <TResult>(
                      _proxyType,
                      typeof(TResult),
                      bindingsToTargetType,
                      castedSource,
                      runtimeParameter
                      );

            // Return list of T
            return(targetTypeSelectExpressionQuery.ToList());
        }
        private IQueryable <object> ToObjectQuery(IQueryable <TResult> query)
        {
            IEnumerable <string> selectedPropertyNames = (_fields ?? "").ToLowerInvariant().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim());

            // Take properties from the mapped entitiy that match selected properties
            _selectedProperties   = GetSelectableProperties <TResult>(selectedPropertyNames);
            selectedPropertyNames = _selectedProperties.Keys;

            // Construct runtime type by given property configuration
            _proxyType = ProxyGenerator.CreateClassProxy <TResult>().GetType();

            // Create instance of source parameter
            ParameterExpression sourceParameter = Expression.Parameter(typeof(TResult), "t");

            // Take fields from generated runtime type
            _proxyProperties = _proxyType.GetProperties();

            // Elect selected fields if any.
            if (selectedPropertyNames.Any())
            {
                _proxyProperties = _proxyProperties.Where(pi => selectedPropertyNames.Any(p => pi.Name.ToLowerInvariant() == p)).ToArray();
            }

            // Generate bindings from source type to runtime type
            IEnumerable <MemberBinding> bindingsToRuntimeType = _proxyProperties
                                                                .Select(field => Expression.Bind(
                                                                            field,
                                                                            Expression.Property(
                                                                                sourceParameter,
                                                                                _selectedProperties[field.Name.ToLowerInvariant()]
                                                                                )
                                                                            ));

            // Generate projection trom T to runtimeType and cast as IQueryable<object>
            IQueryable <object> runtimeTypeSelectExpressionQuery
                = ExpressionMapper.GenerateProjectedQuery <object>(
                      typeof(TResult),
                      _proxyType,
                      bindingsToRuntimeType,
                      query,
                      sourceParameter
                      );

            return(runtimeTypeSelectExpressionQuery);
        }