/// <summary> /// Does the current iterator have another batch of data to process? /// </summary> /// <returns>true if more data can be accessed via <code>CurrentBatch</code></returns> /// <remarks> /// Creates a new database context, issues a query, and places a materialized collection in <code>CurrentBatch</code>. /// Context is disposed once the query is issued. /// Materialized collection is specified by <code>BuildIQueryable</code>. Use of any associated navigation properties /// must be accounted for by using the appropriate <code>.Include</code> operator where the query is /// built in <code>BuildIQueryable</code>. /// </remarks> public bool HasNext() { using (YourObjectContextHere db = new YourObjectContextHere()) { this.currentBatch = this.BuildIQueryable(db) .OrderBy(this.orderBy) .Skip(this.currentIndex) .Take(this.batchSize) .ToList() .AsParallel(); this.currentIndex += this.batchSize; return(currentBatch.Count() > 0); } }
/// <summary> /// Given a Database Context, builds a query which can be executed in batches. /// </summary> /// <param name="db">context on which to build and execute the query</param> /// <returns>a query which will be executed and materialized</returns> /// <remarks>Context will be disposed as soon a HasNext has been executed.</remarks> protected abstract IQueryable <T> BuildIQueryable(YourObjectContextHere db);
protected override IQueryable<MyObject> BuildIQueryable(YourObjectContextHere db) { IQueryable<MyObject> myObjects= db.SomeCollection.Select(x => this.instanceIds.Contains(x).Include("SomethingImportant"); return myObjects; }