var results = from myRow in dtTaskandBugs.AsParallel()
select myRow;
results.ForAll(r =>
{
r["Storyid"] = GetStoryid(r["Id"]);
r["FeatureID"] = Fidname(r["Storyid"]);
r["FeatureName"] = r["FeatureID"].ToString() == "0" ?
"Anonymous" :
fname(r["FeatureID"]);
});
PLINQ could help if performance is an issue. It actually determines whether it expects a speed-up by running the query in parallel and will run parallel or sequentially accordingly.
If you have control over the
GetStoryid()
,
Fidname()
, or
fname()
functions you could use
async
/
await
. If they have heavy delay (e.g. accessing another DB or something) this might help:
public async Task<int> GetStoryid(int id)
{
Task<int> processing = Task.Run(() => { });
int result = await processing;
return result;
}
var results = from myRow in dtTaskandBugs.AsParallel()
select myRow;
results.ForAll(async r =>
{
Task<int> storyProcessing = GetStoryid(r["Id"]);
Task<int> fidProcessing = Fidname(r["Storyid"]);
Task<string> featureProcessing = r["FeatureID"].ToString() == "0" ?
Task.FromResult("Anonymous") :
fname(r["FeatureID"]);
r["Storyid"] = await storyProcessing;
r["FeatureID"] = await fidProcessing;
r["FeatureName"] = await featureProcessing;
});
EDIT: original edit deleted for space.
EDIT2: So after reading more into
Query
and seeing below that PLINQ isn't helping, I'm guessing the DB calls are your biggest problem. The code below demonstrates a working example of what my suggestion would be (
async
) along with comments to help you to get it to work:
class Program
{
private static Random randomNumber = new Random();
static void Main(string[] args)
{
int tableRows = 10;
List<Dictionary<string, int>> table = new List<Dictionary<string, int>>();
for (int i = 0; i < tableRows; i++)
{
Dictionary<string, int> row = new Dictionary<string, int>();
row.Add("Id", i);
row.Add("StoryId", tableRows + i);
row.Add("FeatureId", tableRows * 2 + i);
table.Add(row);
}
var results = from myRow in table
select myRow;
List<Task> tasks = new List<Task>();
foreach (var result in results)
tasks.Add(Process(result));
Task.WaitAll(tasks.ToArray());
Console.ReadKey();
}
public static async Task Process(Dictionary<string, int> row)
{
Task<int> storyProcessing = GetStoryid(row["StoryId"]);
Task<int> fidProcessing = Fidname(row["FeatureId"]);
Task<string> nameProcessing = fname(row["Id"]);
await Task.WhenAll(storyProcessing, fidProcessing, nameProcessing);
Console.WriteLine($"#{row["Id"]} complete. SID: {storyProcessing.Result}, FID: {fidProcessing.Result}, FN: {nameProcessing.Result}");
}
public static async Task<int> GetStoryid(int id)
{
await Task.Delay(randomNumber.Next(10000));
return id * 10;
}
public static async Task<int> Fidname(int id)
{
await Task.Delay(randomNumber.Next(10000));
return id * 10;
}
public static async Task<string> fname(int id)
{
await Task.Delay(randomNumber.Next(10000));
return $"{id * 10}";
}
}
More info:
Task-based Asynchronous Programming (TAP)[
^],
APM to TAP[
^], and
Query[
^].