public async Task <List <TResult> > ExecuteAsync <TResult>(Func <IQueryable, Task <TResult> > efQuery) { if (_mergeContext.Skip.HasValue || _mergeContext.Take.HasValue) { throw new InvalidOperationException("aggregate not support skip take"); } //从各个分表获取数据 List <DbContext> parallelDbContexts = new List <DbContext>(_mergeContext.RouteResults.Count()); try { var enumeratorTasks = _mergeContext.RouteResults.Select(routeResult => { return(Task.Run(async() => { var shardingDbContext = _mergeContext.CreateDbContext(); parallelDbContexts.Add(shardingDbContext); var newQueryable = (IQueryable <T>)_mergeContext.GetReWriteQueryable().ReplaceDbContextQueryable(shardingDbContext); return await EFCoreExecute(newQueryable, routeResult, efQuery); })); }).ToArray(); return((await Task.WhenAll(enumeratorTasks)).ToList()); } finally { parallelDbContexts.ForEach(o => o.Dispose()); } }
public async Task <IStreamMergeAsyncEnumerator <T> > GetStreamEnumerator() { var enumeratorTasks = _mergeContext.RouteResults.Select(routeResult => { return(Task.Run(async() => { var shardingDbContext = _mergeContext.CreateDbContext(); _parallelDbContexts.Add(shardingDbContext); var newQueryable = (IQueryable <T>)_mergeContext.GetReWriteQueryable().ReplaceDbContextQueryable(shardingDbContext); var asyncEnumerator = await GetAsyncEnumerator(newQueryable, routeResult); return new StreamMergeAsyncEnumerator <T>(asyncEnumerator); })); }).ToArray(); var streamEnumerators = await Task.WhenAll(enumeratorTasks); if (_mergeContext.HasSkipTake()) { return(new NoPaginationStreamMergeEnumerator <T>(_mergeContext, streamEnumerators)); } if (_mergeContext.HasGroupQuery()) { return(new MultiAggregateOrderStreamMergeAsyncEnumerator <T>(_mergeContext, streamEnumerators)); } return(new MultiOrderStreamMergeAsyncEnumerator <T>(_mergeContext, streamEnumerators)); }