/// <summary>Enumerates create enumerable in this collection.</summary>
        /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
        /// <returns>
        ///     An enumerator that allows foreach to be used to process create enumerable in this collection.
        /// </returns>
        public IEnumerable <T> CreateEnumerable()
        {
            QueryIncludeOptimizedIncludeSubPath.RemoveLazyChild(this);

            // MODIFY query if necessary
#if EF5 || EF6
            var objectContext = OriginalQueryable.GetObjectQuery().Context;
            var keyMembers    = ((dynamic)objectContext).CreateObjectSet <T>().EntitySet.ElementType.KeyMembers;
            var keyNames      = ((IEnumerable <EdmMember>)keyMembers).Select(x => x.Name).ToArray();
#elif EFCORE
            var context = currentQuery.OriginalQueryable.GetDbContext();

            var keyNames = context.Model.FindEntityType(typeof(TResult).DisplayName(true))
                           .GetKeys().ToList()[0]
                           .Properties.Select(x => x.Name).ToArray();
#endif
            var newQuery = OriginalQueryable.AddToRootOrAppendOrderBy(keyNames).Select(x => x);

            foreach (var child in Childs)
            {
                child.CreateIncludeQuery(newQuery);
            }

            // RESOLVE current and all future child queries
            var list = QueryIncludeOptimizedManager.AllowQueryBatch ? newQuery.Future().ToList() : newQuery.ToList();

            // FIX collection null
            QueryIncludeOptimizedNullCollection.NullCollectionToEmpty(list, Childs);

            return(list);
        }
        /// <summary>Enumerates create enumerable in this collection.</summary>
        /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
        /// <returns>
        ///     An enumerator that allows foreach to be used to process create enumerable in this collection.
        /// </returns>
        public IEnumerable <T> CreateEnumerable()
        {
            // MODIFY query if necessary
#if EF5 || EF6
            var objectContext = OriginalQueryable.GetObjectQuery().Context;
            var keyMembers    = ((dynamic)objectContext).CreateObjectSet <T>().EntitySet.ElementType.KeyMembers;
            var keyNames      = ((IEnumerable <EdmMember>)keyMembers).Select(x => x.Name).ToArray();
#elif EFCORE
            var context = currentQuery.OriginalQueryable.GetDbContext();

            var keyNames = context.Model.FindEntityType(typeof(TResult).DisplayName(true))
                           .GetKeys().ToList()[0]
                           .Properties.Select(x => x.Name).ToArray();
#endif
            var newQuery = OriginalQueryable.AddToRootOrAppendOrderBy(keyNames).Select(x => x);

            foreach (var child in Childs)
            {
                child.CreateIncludeQuery(newQuery);
            }

            // RESOLVE current and all future child queries
            return(newQuery.Future().ToList());
        }
        /// <summary>Enumerates create enumerable in this collection.</summary>
        /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
        /// <returns>
        ///     An enumerator that allows foreach to be used to process create enumerable in this collection.
        /// </returns>
        public IEnumerable <T> CreateEnumerable()
        {
            //var objectQuery = OriginalQueryable.GetObjectQuery();


            //if (objectQuery.MergeOption == MergeOption.NoTracking)
            //{
            //    objectQuery.MergeOption = MergeOption.AppendOnly;

            //    var newContext = QueryIncludeOptimizedManager.DbContextFactory(objectQuery.Context.GetDbContext()).GetObjectContext();

            //    // CHANGE the context under the objectQuery
            //    {
            //        var internalQueryProperty = OriginalQueryable.GetType().GetProperty("InternalQuery", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            //        var internalQuery = internalQueryProperty.GetValue(OriginalQueryable);
            //        //var internalQueryProperty = typeof(DbQuery).GetProperty("InternalQuery", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            //        //var internalQuery = internalQueryProperty.GetValue(OriginalQueryable, null);

            //        var stateField = typeof(ObjectQuery).GetField("_state", BindingFlags.NonPublic | BindingFlags.Instance);
            //        var state = stateField.GetValue(objectQuery);

            //        var assembly = typeof(ObjectQuery).Assembly;
            //        var objectQueryState = assembly.GetType("System.Data.Entity.Core.Objects.Internal.ObjectQueryState");
            //        var contextField = objectQueryState.GetField("_context", BindingFlags.NonPublic | BindingFlags.Instance);
            //        contextField.SetValue(state, newContext);

            //        var expressionField = state.GetType().GetField("_expression", BindingFlags.NonPublic | BindingFlags.Instance);
            //        var expression = (Expression)expressionField.GetValue(state);

            //        var visitor = new QueryIncludeOptimizedExpressionReduceVisitor2();
            //        expression = visitor.Visit(expression);
            //    }
            //}


            QueryIncludeOptimizedIncludeSubPath.RemoveLazyChild(this);

            // MODIFY query if necessary
#if EF5 || EF6
            var objectContext = OriginalQueryable.GetObjectQuery().Context;

            var keyMembers = objectContext.GetEntitySet <T>().ElementType.KeyMembers;
            var keyNames   = keyMembers.Select(x => x.Name).ToArray();
#elif EFCORE
            var context = currentQuery.OriginalQueryable.GetDbContext();

            var keyNames = context.Model.FindEntityType(typeof(TResult).DisplayName(true))
                           .GetKeys().ToList()[0]
                           .Properties.Select(x => x.Name).ToArray();
#endif
            var newQuery = OriginalQueryable.AddToRootOrAppendOrderBy(keyNames).Select(x => x);

            List <T> list;

            if (QueryIncludeOptimizedManager.AllowQueryBatch)
            {
                var future = newQuery.Future();

                foreach (var child in Childs)
                {
                    child.CreateIncludeQuery(newQuery);
                }

                list = future.ToList();
            }
            else
            {
                list = newQuery.ToList();

                foreach (var child in Childs)
                {
                    child.CreateIncludeQuery(newQuery);
                }
            }

#if EF6
            // FIX lazy loading
            QueryIncludeOptimizedLazyLoading.SetLazyLoaded(list, Childs);
#endif

            // FIX collection null
            QueryIncludeOptimizedNullCollection.NullCollectionToEmpty(list, Childs);

            return(list);
        }
Exemplo n.º 4
0
        public async Task <List <T> > CreateEnumerableAsync(CancellationToken cancellationToken)
        {
            QueryIncludeOptimizedIncludeSubPath.RemoveLazyChild(this);

            // MODIFY query if necessary
#if EF5 || EF6
            var objectContext = OriginalQueryable.GetObjectQuery().Context;

            var keyMembers = objectContext.GetEntitySet <T>().ElementType.KeyMembers;
            var keyNames   = keyMembers.Select(x => x.Name).ToArray();
#elif EFCORE
            DbContext context = null;

            if (OriginalQueryable.IsInMemoryQueryContext())
            {
                context = OriginalQueryable.GetInMemoryContext();
            }
            else
            {
                // MODIFY query if necessary
                context = OriginalQueryable.GetDbContext();
            }

            var keyNames = context.Model.FindEntityType(typeof(T).DisplayName(true))
                           .GetKeys().ToList()[0]
                           .Properties.Select(x => x.Name).ToArray();
#endif
            var newQuery = OriginalQueryable.AddToRootOrAppendOrderBy(keyNames).Select(x => x);

            List <T> list;

            if (QueryIncludeOptimizedManager.AllowQueryBatch)
            {
                var future = newQuery.Future();

                foreach (var child in Childs)
                {
                    child.CreateIncludeQuery(newQuery);
                }

                list = await future.ToListAsync(cancellationToken).ConfigureAwait(false);
            }
            else
            {
                list = await newQuery.ToListAsync(cancellationToken).ConfigureAwait(false);

                foreach (var child in Childs)
                {
                    child.CreateIncludeQuery(newQuery);
                }
            }

#if EF6
            // FIX lazy loading
            QueryIncludeOptimizedLazyLoading.SetLazyLoaded(list, Childs);
#endif

            // FIX collection null
            QueryIncludeOptimizedNullCollection.NullCollectionToEmpty(list, Childs);

            return(list);
        }