/// <summary>Creates ordered queryable.</summary> /// <typeparam name="TResult">Type of the result.</typeparam> /// <param name="originalQuery">The original query.</param> /// <returns>The new ordered queryable.</returns> public virtual BatchOrderedQueryable <TResult> CreateOrderedQueryable <TResult>(IOrderedQueryable <TResult> originalQuery, bool updateChild = true) { OwnerBatch.Queries.Remove(this); var query = new BatchOrderedQueryable <TResult>(originalQuery); query.OwnerBatch = OwnerBatch; OwnerBatch.Queries.Add(query); if (updateChild) { Childs.ForEach(x => x.OwnerParent = query); } return(query); }
/// <summary>An IQueryable<T> extension method that includes.</summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <typeparam name="T2">Generic type parameter.</typeparam> /// <param name="source">The source to act on.</param> /// <param name="selector">The selector.</param> /// <param name="useOptimizedInclude">true to use optimized include.</param> /// <returns>An IQueryable<T></returns> public static IQueryable <T> Include <T, T2>(this IQueryable <T> source, Expression <Func <T, IEnumerable <T2> > > selector, bool useOptimizedInclude) where T : class where T2 : class { if (useOptimizedInclude) { var includeOrderedQueryable = source as BatchOrderedQueryable <T>; ObjectQuery objectQuery; if (includeOrderedQueryable == null) { includeOrderedQueryable = new BatchOrderedQueryable <T>(source); var batch = new BatchQuery { Context = source.GetObjectQuery().Context }; includeOrderedQueryable.OwnerBatch = batch; batch.Queries.Add(includeOrderedQueryable); objectQuery = source.GetObjectQuery(); } else { objectQuery = includeOrderedQueryable.ObjectQuery; } // CREATE include query var includeQuery2 = objectQuery.Context.CreateObjectSet <T2>(); Func <ObjectQuery, IQueryable <T2> > queryFactory = x => includeQuery2.Intersect(((ObjectQuery <T>)x).SelectMany(selector)); var includeQueryable2 = new BatchOrderedQueryable <T2>(queryFactory); // Link includeOrderedQueryable.OwnerBatch.Queries.Add(includeQueryable2); includeQueryable2.OwnerParent = includeOrderedQueryable; includeOrderedQueryable.Childs.Add(includeQueryable2); return(includeOrderedQueryable); } return(source.Include(selector)); }