本文整理汇总了C#中System.Threading.Tasks.Task.Select方法的典型用法代码示例。如果您正苦于以下问题:C# Task.Select方法的具体用法?C# Task.Select怎么用?C# Task.Select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.Tasks.Task
的用法示例。
在下文中一共展示了Task.Select方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SpinParallel
/// <summary>
/// Runs given function in parallel on multiple tasks.
/// Number of tasks spawned matches number of logical processors.
/// </summary>
/// <param name="millisecondsToRun"></param>
/// <param name="funcToTest"></param>
/// <returns></returns>
public static long SpinParallel(long millisecondsToRun, Action funcToTest)
{
Task<long>[] tasks = new Task<long>[Environment.ProcessorCount];
for(int i = 0; i < Environment.ProcessorCount; i++)
{
tasks[i] = new Task<long>(() => Spin(millisecondsToRun, funcToTest));
}
DateTime start = DateTime.UtcNow;
tasks.ForEach(task => task.Start());
// ReSharper disable once CoVariantArrayConversion
Task.WaitAll(tasks);
long ranMillisec = (long)(DateTime.UtcNow - start).TotalMilliseconds;
Debug.WriteLine("Parallel call counting run time mismatch is {0:#,#} milliseconds.", ranMillisec - millisecondsToRun);
long[] runCounts = tasks.Select(thread => thread.Result).ToArray();
long total = runCounts.Sum();
return total;
}
示例2: Search
/// <summary> A search implementation allowing sorting which spans a new thread for each
/// Searchable, waits for each search to complete and merges
/// the results back together.
/// </summary>
public override TopFieldDocs Search(Weight weight, Filter filter, int nDocs, Sort sort)
{
if (sort == null) throw new ArgumentNullException("sort");
FieldDocSortedHitQueue hq = new FieldDocSortedHitQueue(nDocs);
object lockObj = new object();
Task<TopFieldDocs>[] tasks = new Task<TopFieldDocs>[searchables.Length];
for (int i = 0; i < searchables.Length; i++) // search each searchable
{
int cur = i;
tasks[i] =
Task<TopFieldDocs>.Factory.StartNew(
() => MultiSearcherCallableWithSort(ThreadLock.MonitorLock, lockObj, searchables[cur], weight, filter, nDocs, hq, sort, cur,
starts));
}
int totalHits = 0;
float maxScore = float.NegativeInfinity;
Task.WaitAll(tasks);
foreach (TopFieldDocs topFieldDocs in tasks.Select(x => x.Result))
{
totalHits += topFieldDocs.TotalHits;
maxScore = Math.Max(maxScore, topFieldDocs.MaxScore);
}
ScoreDoc[] scoreDocs = new ScoreDoc[hq.Size()];
for (int i = hq.Size() - 1; i >= 0; i--)
scoreDocs[i] = hq.Pop();
return new TopFieldDocs(totalHits, scoreDocs, hq.GetFields(), maxScore);
}
示例3: ValidateAndSaveAsync
public Task<MasterDataImportResponse> ValidateAndSaveAsync(IEnumerable<ImportEntity> imports)
{
return Task.Run(() =>
{
var response = new MasterDataImportResponse();
var importEntities = imports as List<ImportEntity> ?? imports.ToList();
int batchSize = Convert.ToInt32(0.2*importEntities.Count());
var productImports =
Enumerable.Select<IEnumerable<ImportEntity>, List<ImportEntity>>(importEntities.OrderBy(p => p.MasterDataCollective).Batch(batchSize), x => Enumerable.ToList<ImportEntity>(x)).ToList();
validationResultInfos.Clear();
#region Construct Pricings
var pricingTaskArray = new Task<ProductPricingDTO[]>[productImports.Count];
var results = new List<ProductPricingDTO>();
try
{
for (int i = 0; i < pricingTaskArray.Length; i++)
{
var current = productImports.FirstOrDefault();
if (current != null && current.Any())
{
pricingTaskArray[i] =
Task<ProductPricingDTO[]>.Factory.StartNew(
() => ConstructDtOs(current));
productImports.Remove(current);
}
}
foreach (var result in pricingTaskArray.Select(n => n.Result).ToList())
{
results.AddRange(result);
}
}
catch (AggregateException ex)
{
}
#endregion
#region validate pricings
var validationResults = new List<ImportValidationResultInfo>();
if (validationResultInfos.Any())
{
validationResults.AddRange(validationResultInfos);
validationResultInfos.Clear();
}
if (results.Any())
{
batchSize = Convert.ToInt32(0.2*results.Count);
var productPricings =
results.OrderBy(p => p.SellingPrice).Batch(batchSize).Select(
x => x.ToList()).ToList();
var validationTaskArray =
new Task<ImportValidationResultInfo[]>[productPricings.Count];
try
{
for (int i = 0; i < validationTaskArray.Length; i++)
{
var current = productPricings.FirstOrDefault();
if (current != null && current.Any())
{
validationTaskArray[i] =
Task<ImportValidationResultInfo[]>.Factory.StartNew(
() => MapAndValidate(current));
productPricings.Remove(current);
}
}
foreach (var result in validationTaskArray.Select(n => n.Result).ToList())
{
validationResults.AddRange(result);
}
}
catch (AggregateException ex)
{
}
}
#endregion
#region Save pricings
var validatedPricings =
validationResults.Where(n => n.Entity is ProductPricing && n.IsValid).Select
(n => (ProductPricing) n.Entity).ToList();
if (validatedPricings.Any())
{
batchSize = Convert.ToInt32(0.2*validatedPricings.Count);
var pricings =
validatedPricings.OrderBy(p => p.CurrentEffectiveDate).Batch(batchSize).
Select(x => x.ToList()).ToList();
var saveTasksArray = new Task<Guid[]>[pricings.Count];
try
{
for (int i = 0; i < saveTasksArray.Length; i++)
{
//.........这里部分代码省略.........
示例4: ValidateAndSave
public IList<ImportValidationResultInfo> ValidateAndSave(List<ProductDiscountGroupImport> entities = null)
{
int batchSize = Convert.ToInt32(0.2 * entities.Count);
var productDiscountImports = entities.OrderBy(p => p.ProductCode).Batch(batchSize).Select(x => x.ToList()).ToList();
#region Contruct Items
var taskArray = new Task<IEnumerable<ProductGroupDiscount>>[productDiscountImports.Count];
var results = new List<ProductGroupDiscount>();
try
{
for (int i = 0; i < taskArray.Length; i++)
{
var current = productDiscountImports.FirstOrDefault();
if (current != null && current.Any())
{
taskArray[i] = Task<IEnumerable<ProductGroupDiscount>>.Factory.StartNew(() => ConstructDomainEntities(current));
productDiscountImports.Remove(current);
}
}
foreach (var result in taskArray.Select(n => n.Result).ToList())
{
results.AddRange(result);
}
}
catch (AggregateException ex)
{
}
#endregion
#region Validate
var validationResults = new List<ImportValidationResultInfo>();
if (results.Any())
{
batchSize = Convert.ToInt32(0.2 * results.Count);
var productDiscounts = results.OrderBy(p => p._DateLastUpdated).Batch(batchSize).Select(x => x.ToList()).ToList();
var validationTaskArray = new Task<ImportValidationResultInfo[]>[productDiscounts.Count];
try
{
for (int i = 0; i < validationTaskArray.Length; i++)
{
var current = productDiscounts.FirstOrDefault();
if (current != null && current.Any())
{
validationTaskArray[i] =
Task<ImportValidationResultInfo[]>.Factory.StartNew(() => ValidateDiscounts(current));
productDiscounts.Remove(current);
}
}
foreach (var result in validationTaskArray.Select(n => n.Result).ToList())
{
validationResults.AddRange(result);
}
}
catch (AggregateException ex)
{
}
}
#endregion
#region Save valid Items
var validatedProductDiscounts = validationResults.Where(n => n.IsValid).Select(n => (ProductGroupDiscount)n.Entity).ToList();
if (validatedProductDiscounts.Any())
{
batchSize = Convert.ToInt32(0.2 * validatedProductDiscounts.Count);
var products =
validatedProductDiscounts.OrderBy(p => p._DateLastUpdated).Batch(batchSize).Select(x => x.ToList()).ToList();
var saveTasksArray = new Task<Guid[]>[products.Count];
try
{
for (int i = 0; i < saveTasksArray.Length; i++)
{
var current = products.FirstOrDefault();
if (current != null && current.Any())
{
saveTasksArray[i] =
Task<Guid[]>.Factory.StartNew(() => SaveDiscounts(current));
products.Remove(current);
}
}
var savedResults = new List<Guid>();
foreach (var result in saveTasksArray.Select(n => n.Result).ToList())
{
savedResults.AddRange(result);
}
}
catch (AggregateException ex)
{
}
}
#endregion
return validationResults.Where(p => !p.IsValid).ToList();
}
示例5: ValidateAndSave
public IList<ImportValidationResultInfo> ValidateAndSave(List<ProductBrandImport> entities = null)
{
int batchSize = Convert.ToInt32(0.2 * entities.Count);
var brandImports = entities.OrderBy(p => p.Code).Batch(batchSize).Select(x => x.ToList()).ToList();
#region Construct Items
var taskArray = new Task<ProductBrand[]>[brandImports.Count];
var results = new List<ProductBrand>();
try
{
for (int i = 0; i < taskArray.Length; i++)
{
var current = brandImports.FirstOrDefault();
if (current != null && current.Any())
{
taskArray[i] = Task<ProductBrand[]>.Factory.StartNew(() => ConstructEntities(current));
brandImports.Remove(current);
}
}
foreach (var result in taskArray.Select(n => n.Result).ToList())
{
results.AddRange(result);
}
}
catch (AggregateException ex)
{
}
#endregion
#region Validate Items
var validationResults = new List<ImportValidationResultInfo>();
if (results.Any())
{
batchSize = Convert.ToInt32(0.2 * results.Count);
var brands = results.OrderBy(p => p.Code).Batch(batchSize).Select(x => x.ToList()).ToList();
var validationTaskArray = new Task<ImportValidationResultInfo[]>[brands.Count];
try
{
for (int i = 0; i < validationTaskArray.Length; i++)
{
var current = brands.FirstOrDefault();
if (current != null && current.Any())
{
validationTaskArray[i] =
Task<ImportValidationResultInfo[]>.Factory.StartNew(() => ValidateProductBrands(current));
brands.Remove(current);
}
}
foreach (var result in validationTaskArray.Select(n => n.Result).ToList())
{
validationResults.AddRange(result);
}
}
catch (AggregateException ex)
{
}
}
#endregion
#region Save valid items
var validatedBrands = validationResults.Where(n => n.IsValid).Select(n => (ProductBrand)n.Entity).ToList();
if (validatedBrands.Any())
{
batchSize = Convert.ToInt32(0.2 * validatedBrands.Count);
var brands =
validatedBrands.OrderBy(p => p.Code).Batch(batchSize).Select(x => x.ToList()).ToList();
var saveTasksArray = new Task<Guid[]>[brands.Count];
try
{
for (int i = 0; i < saveTasksArray.Length; i++)
{
var current = brands.FirstOrDefault();
if (current != null && current.Any())
{
saveTasksArray[i] =
Task<Guid[]>.Factory.StartNew(() => SaveProductBrands(current));
brands.Remove(current);
}
}
var savedResults = new List<Guid>();
foreach (var result in saveTasksArray.Select(n => n.Result).ToList())
{
savedResults.AddRange(result);
}
}
catch (AggregateException ex)
{
}
}
return validationResults.Where(p => !p.IsValid).ToList();
#endregion
}
示例6: ValidateAndSave
public IList<ImportValidationResultInfo> ValidateAndSave(List<ShipToAddressImport> entities)
{
int batchSize = Convert.ToInt32(0.2 * entities.Count);
var addressImports = entities.OrderBy(p => p.OutletCode).Batch(batchSize).Select(x => x.ToList()).ToList();
#region Contruct outlets
var taskArray = new Task<bool>[addressImports.Count];
var results = new List<bool>();
try
{
for (int i = 0; i < taskArray.Length; i++)
{
var current = addressImports.FirstOrDefault();
if (current != null && current.Any())
{
taskArray[i] = Task<bool>.Factory.StartNew(() => ConstructValidateAndUpdate(current));
addressImports.Remove(current);
}
}
results.AddRange(taskArray.Select(n => n.Result).ToList());
}
catch (AggregateException ex)
{
}
catch(Exception exception)
{
}
#endregion
return validations;
}
示例7: ValidateAndSave
public IList<ImportValidationResultInfo> ValidateAndSave(List<ProductImport> entities = null)
{
int batchSize = Convert.ToInt32(0.2 * entities.Count);
var productImports = entities.OrderBy(p => p.ProductCode).Batch(batchSize).Select(x => x.ToList()).ToList();
#region Contruct Items
var taskArray = new Task<SaleProduct[]>[productImports.Count];
var results = new List<SaleProduct>();
try
{
for (int i = 0; i < taskArray.Length; i++)
{
var current = productImports.FirstOrDefault();
if (current != null && current.Any())
{
taskArray[i] = Task<SaleProduct[]>.Factory.StartNew(() => ConstructProducts(current));
productImports.Remove(current);
}
}
foreach (var result in taskArray.Select(n => n.Result).ToList())
{
results.AddRange(result);
}
}
catch (AggregateException ex)
{
}
#endregion
#region Validate
var validationResults = new List<ImportValidationResultInfo>();
if (results.Any())
{
batchSize = Convert.ToInt32(0.2 * results.Count);
var products = results.OrderBy(p => p.ProductCode).Batch(batchSize).Select(x => x.ToList()).ToList();
var validationTaskArray = new Task<ImportValidationResultInfo[]>[products.Count];
try
{
for (int i = 0; i < validationTaskArray.Length; i++)
{
var current = products.FirstOrDefault();
if (current != null && current.Any())
{
validationTaskArray[i] =
Task<ImportValidationResultInfo[]>.Factory.StartNew(() => ValidateProducts(current));
products.Remove(current);
}
}
foreach (var result in validationTaskArray.Select(n => n.Result).ToList())
{
validationResults.AddRange(result);
}
}
catch (AggregateException ex)
{
}
}
#endregion
#region Save valid Items
var validatedSaleProducts = validationResults.Where(n => n.IsValid).Select(n => (SaleProduct)n.Entity).ToList();
if (validatedSaleProducts.Any())
{
batchSize = Convert.ToInt32(0.2 * validatedSaleProducts.Count);
var products =
validatedSaleProducts.OrderBy(p => p.ProductCode).Batch(batchSize).Select(x => x.ToList()).ToList();
var saveTasksArray = new Task<Guid[]>[products.Count];
try
{
for (int i = 0; i < saveTasksArray.Length; i++)
{
var current = products.FirstOrDefault();
if (current != null && current.Any())
{
saveTasksArray[i] =
Task<Guid[]>.Factory.StartNew(() => SaveProducts(current));
products.Remove(current);
}
}
var savedResults = new List<Guid>();
foreach (var result in saveTasksArray.Select(n => n.Result).ToList())
{
savedResults.AddRange(result);
}
}
catch (AggregateException ex)
{
}
}
#endregion
#region Construct Pricings
//.........这里部分代码省略.........
示例8: ComputeBondSpread
public IEnumerable<BondSpreadMergedResult> ComputeBondSpread(string ccy, string oisCurveName, string liborCurveName)
{
try
{
var nowTime = DateTime.Now;
Log.Info("Prepare for bond spread computation...");
if (futureBuilder.Today.Date < DateTime.UtcNow.AddHours(-5).Date)
{
Log.InfoFormat("previous today date set to {0}, now today date is set to {1}", futureBuilder.Today.Date, DateTime.UtcNow.AddHours(-5).Date);
// when we move forward to the next day, we need to roll dates
futureBuilder.ComputeRelevantDatesForForwards();
futureBuilder.ComputeRollDates();
// recompute closes
histPriceCache.Clear();
futureEnricher.ClearCloseCache();
}
// 1. need to get all bonds statics
//var bondLists = GetBondStatics();
//var allbonds = bondSpreadServiceModel.GetAllSecurities("T", "Term", DateTime.Today.AddDays(30));
//var bondLists = bondPriceCache.Keys.ToArray();
var bondLists = bondStaticCache.Keys.ToArray();
// 2. build curve
curves.SetupCurves(ccy, oisCurveName, liborCurveName);
Log.InfoFormat("Finished curve bulding in {0}secs", (DateTime.Now - nowTime).TotalSeconds);
// 3. compute bond spread
string country = "US";
BondAnalytics.Country eCountry;
BondAnalytics.CountryMappings.TryGetValue(country.ToUpper().Trim(), out eCountry);
long Freq = 6; // hard code to be semi-annual?
// all the bond prices
double optStartDate = 0;
double optFirstCpnDate = 0;
Log.Info("Computing bond spread...");
BondSpreadResult[] liveBondSpreads = null;
BondSpreadResult[] closeBondSpreads = null;
BondSpreadResult[] futureSpreadsLive = null;
BondSpreadResult[] futureSpreadsClose = null;
IEnumerable<BondSpreadMergedResult> MergedBondSpreads = null;
IEnumerable<BondSpreadMergedResult> MergedFuturesSpreads = null;
DateTime futureStart = DateTime.Now;
// future enrichment
lock(lockobj)
{
foreach (var future in FutureCache)
{
try
{
futureEnricher.EnrichFuture(future, curves.GetLiveOISCurve(), curves.GetLiveLiborCurve(), curves.GetCloseOISCurve(), curves.GetCloseLiborCurve(), this); // oiscurve, liborcurve
}
catch (Exception e)
{
Log.Error(string.Format("Failed to enrich future {0}", future.Contract), e);
future.CanComputeSpread = false;
}
}
}
Log.InfoFormat("Finished future enrichment in {0}secs", (DateTime.Now - futureStart).TotalSeconds);
try
{
// compute bond prices in parallel
var starttime = DateTime.Now;
var computingbondlist = bondLists.ToArray();
var closecomputeTasks = new Task<BondSpreadResult>[computingbondlist.Length];
var livecomputeTasks = new Task<BondSpreadResult>[computingbondlist.Length];
//var liveBondSpreads = new List<BondSpreadResult>();
for (int i = 0; i<computingbondlist.Length; i++)
{
var i1 = i;
livecomputeTasks[i] = Task<BondSpreadResult>.Factory.StartNew(
() =>
ComputeLiveBondSpreadResult(computingbondlist[i1], eCountry, optStartDate, optFirstCpnDate, Freq, curves.GetLiveLiborCurve(), curves.GetLiveOISCurve()))
.ContinueWith(r => r.Result);
}
for (int i = 0; i < computingbondlist.Length; i++)
{
var i1 = i;
closecomputeTasks[i] = Task<BondSpreadResult>.Factory.StartNew(
() =>
ComputeCloseBondSpreadResult(computingbondlist[i1], eCountry, optStartDate, optFirstCpnDate, Freq, curves.GetCloseLiborCurve(), curves.GetCloseOISCurve()))
.ContinueWith(r => r.Result);
}
Task.WaitAll(closecomputeTasks.Concat(livecomputeTasks).Cast<Task>().ToArray());
liveBondSpreads = livecomputeTasks.Select(l => l.Result).Where(r=> r != null).ToArray();
//.........这里部分代码省略.........
示例9: GetThreadSpecificGenerator_Returns_IdGenerator
public void GetThreadSpecificGenerator_Returns_IdGenerator()
{
if (Environment.ProcessorCount > 1)
{
const int gcount = 100; //Create a fair amount of generators
var tasks = new Task<IdGenerator>[gcount];
for (int i = 0; i < gcount; i++)
tasks[i] = Task.Run(() => IdGenerator.GetThreadSpecificGenerator());
Task.WaitAll(tasks);
// Get all unique generator ID's in an array
var generatorids = tasks.Select(t => t.Result.Id).Distinct().ToArray();
// Make sure there's at least more than 1 different Id
Assert.IsTrue(generatorids.Length > 1);
}
}
示例10: F2SmallParallel
private int F2SmallParallel(long x1, long x2)
{
var xmin = UpToOdd(Math.Max(1, x1));
var xmax = DownToOdd(Math.Min((long)IntegerMath.FloorSquareRoot(n), x2));
if (threads <= 1)
return F2SmallParallel(0, xmin, xmax, x1, 2);
var xsmall = DownToOdd(Math.Max(xmin, Math.Min(smallCutoff, xmax)));
var s = 0;
for (var x = xmin; x < xsmall; x += 2)
s += IntegerMath.Mobius(x) * T2Parallel(n / ((UInt128)x * (UInt128)x));
var tasks = new Task<int>[threads];
var increment = 2 * threads;
for (var thread = 0; thread < threads; thread++)
{
var worker = thread;
var offset = 2 * thread;
tasks[thread] = Task.Factory.StartNew(() => F2SmallParallel(worker, xsmall + offset, xmax, x1, increment));
}
Task.WaitAll(tasks);
s += tasks.Select(task => task.Result).Sum();
return s & 3;
}
示例11: ValidateAndSaveAsync
public Task<MasterDataImportResponse> ValidateAndSaveAsync(IEnumerable<ImportEntity> imports)
{
return Task.Run(() =>
{
var response = new MasterDataImportResponse();
var importEntities = imports as List<ImportEntity> ?? imports.ToList();
int batchSize = Convert.ToInt32(0.2*importEntities.Count());
var outletImports =
Enumerable.Select<IEnumerable<ImportEntity>, List<ImportEntity>>(importEntities.OrderBy(p => p.MasterDataCollective).Batch(batchSize), x => Enumerable.ToList<ImportEntity>(x)).ToList();
validationResultInfos.Clear();
#region Construct Items
var taskArray = new Task<tblShipToAddress[]>[outletImports.Count];
var results = new List<tblShipToAddress>();
try
{
for (int i = 0; i < taskArray.Length; i++)
{
var current = outletImports.FirstOrDefault();
if (current != null && current.Any())
{
taskArray[i] =
Task<tblShipToAddress[]>.Factory.StartNew(
() => ConstructDtOs(current));
outletImports.Remove(current);
}
}
foreach (var result in taskArray.Select(n => n.Result).ToList())
{
results.AddRange(result);
}
}
catch (AggregateException ex)
{
foreach (var error in ex.InnerExceptions)
{
response.ErrorInfo += error;
}
}
var validationResults = new List<ImportValidationResultInfo>();
if (validationResultInfos.Any())
{
validationResults.AddRange(validationResultInfos);
validationResultInfos.Clear();
}
#endregion
#region Save valid items
var validatedAddresses = results;
if (validatedAddresses.Any())
{
batchSize = Convert.ToInt32(0.2*validatedAddresses.Count);
var outlets =
validatedAddresses.OrderBy(p => p.Name).Batch(batchSize).Select(
x => x.ToList()).ToList();
var saveTasksArray = new Task<int>[outlets.Count];
try
{
for (int i = 0; i < saveTasksArray.Length; i++)
{
var current = outlets.FirstOrDefault();
if (current != null && current.Any())
{
saveTasksArray[i] =
Task<int>.Factory.StartNew(() => MapAndInsert(current));
outlets.Remove(current);
}
}
var savedResults = saveTasksArray.Select(n => n.Result).ToList().ToList();
}
catch (AggregateException ex)
{
foreach (var error in ex.InnerExceptions)
{
response.ErrorInfo += error;
}
}
}
#endregion
response.ValidationResults.AddRange(validationResults);
return response;
});
}
示例12: Build
/// <summary>
/// Builds k-mers from a list of given input sequences.
/// For each sequence in input list, constructs a KmersOfSequence
/// corresponding to the sequence and associated k-mers.
/// </summary>
/// <param name="sequences">List of input sequences.</param>
/// <param name="kmerLength">K-mer length.</param>
/// <returns>List of KmersOfSequence instances.</returns>
public IEnumerable<KmersOfSequence> Build(IEnumerable<ISequence> sequences, int kmerLength)
{
if (kmerLength <= 0)
{
throw new ArgumentException(Properties.Resource.KmerLengthShouldBePositive);
}
if (sequences == null)
{
throw new ArgumentNullException("sequences");
}
Task<KmersOfSequence>[] kmerTasks = new Task<KmersOfSequence>[sequences.Count()];
long ndx = 0;
foreach (ISequence sequence in sequences)
{
ISequence localSequence = sequence;
kmerTasks[ndx] = Task<KmersOfSequence>.Factory.StartNew(
o => this.Build(localSequence, kmerLength), TaskCreationOptions.None);
ndx++;
}
return kmerTasks.Select(t => t.Result);
}
示例13: ValidateAndSaveAsync
public Task<MasterDataImportResponse> ValidateAndSaveAsync(IEnumerable<ImportEntity> imports)
{
return Task.Run(() =>
{
var response = new MasterDataImportResponse();
var importEntities = imports as List<ImportEntity> ?? imports.ToList();
int batchSize = Convert.ToInt32(0.2*importEntities.Count());
var outletImports =
Enumerable.Select<IEnumerable<ImportEntity>, List<ImportEntity>>(importEntities.OrderBy(p => p.MasterDataCollective).Batch(batchSize), x => Enumerable.ToList<ImportEntity>(x)).ToList();
#region Construct Items
var taskArray = new Task<OutletDTO[]>[outletImports.Count];
var results = new List<OutletDTO>();
try
{
for (int i = 0; i < taskArray.Length; i++)
{
var current = outletImports.FirstOrDefault();
if (current != null && current.Any())
{
taskArray[i] =
Task<OutletDTO[]>.Factory.StartNew(
() => ConstructDtOs(current));
outletImports.Remove(current);
}
}
foreach (var result in taskArray.Select(n => n.Result).ToList())
{
results.AddRange(result);
}
}
catch (AggregateException ex)
{
foreach (var error in ex.InnerExceptions)
{
response.ErrorInfo += error;
}
}
#endregion
#region Validate Items
var validationResults = new List<ImportValidationResultInfo>();
if (validationResultInfos.Any())
{
validationResults.AddRange(validationResultInfos);
validationResultInfos.Clear();
}
if (results.Any())
{
batchSize = Convert.ToInt32(0.2*results.Count);
var outlets =
results.OrderBy(p => p.CostCentreCode).Distinct().Batch(batchSize).Select(
x => x.ToList()).
ToList();
var validationTaskArray = new Task<ImportValidationResultInfo[]>[outlets.Count];
try
{
for (int i = 0; i < validationTaskArray.Length; i++)
{
var current = outlets.FirstOrDefault();
if (current != null && current.Any())
{
validationTaskArray[i] =
Task<ImportValidationResultInfo[]>.Factory.StartNew(
() => MapAndValidate(current));
outlets.Remove(current);
}
}
foreach (var result in validationTaskArray.Select(n => n.Result).ToList())
{
validationResults.AddRange(result);
}
}
catch (AggregateException ex)
{
foreach (var error in ex.InnerExceptions)
{
response.ErrorInfo += error;
}
}
}
#endregion
#region Save valid items
var validatedOutlets =
validationResults.Where(n => n.IsValid).Select(n => (Outlet) n.Entity).
ToList();
if (validatedOutlets.Any())
{
//.........这里部分代码省略.........
示例14: ValidateAsync
public Task<MasterDataImportResponse> ValidateAsync(IEnumerable<ImportEntity> imports)
{
return Task.Run(() =>
{
var response = new MasterDataImportResponse();
var importEntities = imports as List<ImportEntity> ??
imports.ToList();
int batchSize = Convert.ToInt32(0.2*importEntities.Count()); //batch 20% of the collection and process in parallel
var brandImports =
Enumerable.Select<IEnumerable<ImportEntity>, List<ImportEntity>>(importEntities.OrderBy(p => p.MasterDataCollective).
Batch(batchSize), x => Enumerable.ToList<ImportEntity>(x)).ToList();
#region Construct Items
var taskArray =
new Task<ProductBrandDTO[]>[brandImports.Count];
var results = new List<ProductBrandDTO>();
try
{
for (int i = 0; i < taskArray.Length; i++)
{
var current = brandImports.FirstOrDefault();
if (current != null && current.Any())
{
taskArray[i] =
Task<ProductBrandDTO[]>.Factory.StartNew(
() => ConstructDtOs(current));
brandImports.Remove(current);
}
}
foreach (
var result in
taskArray.Select(n => n.Result).ToList())
{
results.AddRange(result);
}
}
catch (AggregateException ex)
{
foreach (var error in ex.InnerExceptions)
{
response.ErrorInfo += error;
}
}
#endregion
#region Validate Items
var validationResults = new List<ImportValidationResultInfo>();
if (validationResultInfos.Any())
{
validationResults.AddRange(validationResultInfos);
validationResultInfos.Clear();
}
if (results.Any())
{
batchSize = Convert.ToInt32(0.2*results.Count);
var brands =
results.OrderBy(p => p.Code).Batch(batchSize).Select
(x => x.ToList()).
ToList();
var validationTaskArray =
new Task<ImportValidationResultInfo[]>[brands.Count];
try
{
for (int i = 0; i < validationTaskArray.Length; i++)
{
var current = brands.FirstOrDefault();
if (current != null && current.Any())
{
validationTaskArray[i] =
Task<ImportValidationResultInfo[]>.
Factory.StartNew(
() => MapAndValidate(current));
brands.Remove(current);
}
}
foreach (
var result in
validationTaskArray.Select(n => n.Result).
ToList())
{
validationResults.AddRange(result);
}
}
catch (AggregateException ex)
{
foreach (var error in ex.InnerExceptions)
{
response.ErrorInfo += error;
}
}
}
//.........这里部分代码省略.........
示例15: DownloadAllAnimeFromSublists
private static AnimelistResult DownloadAllAnimeFromSublists(string name)
{
Task<string> watchingTask = GetStringFromAsync("http://myanimelist.net/animelist/" + name + "&status=1");
Task<string> completedTask = GetStringFromAsync("http://myanimelist.net/animelist/" + name + "&status=2");
Task<string> onHoldTask = GetStringFromAsync("http://myanimelist.net/animelist/" + name + "&status=3");
Task<string> droppedTask = GetStringFromAsync("http://myanimelist.net/animelist/" + name + "&status=4");
Task<string>[] tasks = new Task<string>[] { watchingTask, completedTask, onHoldTask, droppedTask };
Task.WaitAll(tasks);
watchingTask.Dispose();
completedTask.Dispose();
onHoldTask.Dispose();
droppedTask.Dispose();
var allAnime = tasks.Select(task => AllAnime(task.Result))
.Where(result => result.Response == AnimelistResponse.Successs)
.SelectMany(i => i.Ratings);
return new AnimelistResult() { Response = AnimelistResponse.Successs, Ratings = allAnime.ToList() };
}