Exemplo n.º 1
0
        /// <summary>Includes the given file.</summary>
        /// <param name="path">Full pathname of the file.</param>
        /// <returns>An IQueryable.</returns>
        public IQueryable Include(string path)
        {
            var objectQuery         = OriginalQueryable.GetObjectQuery();
            var objectQueryIncluded = objectQuery.Include(path);

            return(new QueryInterceptorQueryable(objectQueryIncluded, Visitors));
        }
        /// <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);
        }
Exemplo n.º 3
0
        /// <summary>Gets the enumerator.</summary>
        /// <returns>The enumerator.</returns>
        public IEnumerator <T> GetEnumerator()
        {
            var enumerators = OriginalQueryable.GetEnumerator();



            var      asNoTracking = OriginalQueryable.Expression.ToString().Contains("MergeAs(NoTracking)");
            List <T> list         = new List <T>();

            while (enumerators.MoveNext())
            {
                list.Add(enumerators.Current);
            }
            // todo: add all current objects
            List <object> includedObjects = new List <object>();
            List <object> excludedObjects = new List <object>();

            foreach (var includeFilter in IncludeFilters)
            {
                includeFilter.ApplyFilter(list.Cast <object>().ToList());

                includedObjects.AddRange(includeFilter.GetIncludedList());
                excludedObjects.AddRange(includeFilter.GetExcludedList());
            }

            includedObjects.AddRange(list.Cast <object>());

            excludedObjects = excludedObjects.Except(includedObjects).ToList();

            var context = OriginalQueryable.GetObjectQuery().Context;

            excludedObjects.ForEach(x => context.DeleteObject(x));

            return(list.GetEnumerator());
        }
Exemplo n.º 4
0
        /// <summary>Gets object query.</summary>
        /// <returns>The object query.</returns>
        public virtual ObjectQuery GetObjectQuery()
        {
            if (OriginalQueryable == null && QueryFactory != null)
            {
                OriginalQueryable = QueryFactory(OwnerParent.GetObjectQuery());
            }

            ObjectQuery = OriginalQueryable.GetObjectQuery();
            return(OriginalQueryable.GetObjectQuery());
        }
        /// <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.º 7
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);
        }