gpt4 book ai didi

c# - 2个Linq查询可以并行运行吗?

转载 作者:行者123 更新时间:2023-12-03 12:45:30 26 4
gpt4 key购买 nike

我有 2 个 Linq 查询,第一个 Linq 查询返回 1148 条记录,第二个返回 6667 条记录。他们需要 8 分钟的执行时间。有没有办法让它们在并行运行时更快?

var productbacklogworkitem =
(from w in workItemcollectionList where w.Type.Name == "Product Backlog Item" select new {
Id = w.Id,
Name = w.Title,
FID = (w.WorkItemLinks.Count > 0) ? ((w.WorkItemLinks[0].LinkTypeEnd.Name.ToString() != "Child") ? w.WorkItemLinks[0].TargetId : 0) : 0,
Type = w.Type.Name,
State =w.State,
priorty = Convert.ToInt32(w.Fields["Priority"].Value),
Size = Convert.ToInt32(w.Fields["Effort"].Value),
StoryPoints = Convert.ToInt32(w.Fields["Story Points"].Value),
DoneStatus = w.Fields["Done Status"].Value.ToString(),
StoryOwner = w.Fields["Story Owner"].Value.ToString(),
Assignedto = w.Fields["Assigned To"].Value.ToString(),
StoryAuthor = w.Fields["Story Author"].Value.ToString(),
IterationPath = w.IterationPath
}).ToList();
var taskbugsworkitem =
(from w in workItemcollectionList where (w.Type.Name == "Task" || w.Type.Name == "Bug") && (w.WorkItemLinks.Count > 0) select new {
Id = w.Id,
Name = w.Title,
Type = w.Type.Name,
Storyid = w.WorkItemLinks[0].TargetId,
status = w.State,
IterationPath = w.IterationPath,
Assignedto = w.Fields["Assigned To"].Value.ToString(),
priorty = Convert.ToInt32(w.Fields["Priority"].Value),
effort = Convert.ToInt32(w.Fields["effort"].Value),
Completed = (w.Type.Name== "Task") ? Convert.ToInt32(w.Fields["Completed"].Value):0
}) .ToList();

最佳答案

您可以使用 Task也使两个查询并行。

Task<ResultClass1> t1 = Task<ResultClass1>.Run(() =>
{
var productbacklogworkitem =
(from w in workItemcollectionList
where w.Type.Name == "Product Backlog Item"
select new ResultClass1
{
Id = w.Id,
Name = w.Title,
FID = (w.WorkItemLinks.Count > 0) ? ((w.WorkItemLinks[0].LinkTypeEnd.Name.ToString() != "Child") ? w.WorkItemLinks[0].TargetId : 0) : 0,
Type = w.Type.Name,
State = w.State,
priorty = Convert.ToInt32(w.Fields["Priority"].Value),
Size = Convert.ToInt32(w.Fields["Effort"].Value),
StoryPoints = Convert.ToInt32(w.Fields["Story Points"].Value),
DoneStatus = w.Fields["Done Status"].Value.ToString(),
StoryOwner = w.Fields["Story Owner"].Value.ToString(),
Assignedto = w.Fields["Assigned To"].Value.ToString(),
StoryAuthor = w.Fields["Story Author"].Value.ToString(),
IterationPath = w.IterationPath
}).ToList();

return ResultClass1;
});

Task<ResultClass2> t2 = Task<ResultClass2>.Run(() =>
{
var taskbugsworkitem =
(from w in workItemcollectionList
where (w.Type.Name == "Task" || w.Type.Name == "Bug") && (w.WorkItemLinks.Count > 0)
select new ResultClass2
{
Id = w.Id,
Name = w.Title,
Type = w.Type.Name,
Storyid = w.WorkItemLinks[0].TargetId,
status = w.State,
IterationPath = w.IterationPath,
Assignedto = w.Fields["Assigned To"].Value.ToString(),
priorty = Convert.ToInt32(w.Fields["Priority"].Value),
effort = Convert.ToInt32(w.Fields["effort"].Value),
Completed = (w.Type.Name == "Task") ? Convert.ToInt32(w.Fields["Completed"].Value) : 0
}).ToList();

return taskbugsworkitem;
});

Task.WaitAll(t1, t2);

// get results from these
t1.Result;
t2.Result;

关于c# - 2个Linq查询可以并行运行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49001691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com