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); }
/// <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 = QueryIncludeFilterManager.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 QueryIncludeFilterExpressionReduceVisitor2(); // expression = visitor.Visit(expression); // } //} QueryIncludeFilterIncludeSubPath.RemoveLazyChild(this); 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(); //var newQuery = OriginalQueryable.AddToRootOrAppendOrderBy(keyNames).Select(x => x); var newQuery = OriginalQueryable.AddToRootOrAppendOrderBy(keyNames); List <T> list; if (QueryIncludeFilterManager.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); } } // FIX collection null QueryIncludeFilterNullCollection.NullCollectionToEmpty(list, Childs); return(list); }