2 Linq查询可以并行运行吗?

时间:2018-02-27 05:26:15

标签: c# multithreading linq tfs-workitem plinq

我有2个Linq查询,第1个Linq查询返回1148条记录,第2条返回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();

2 个答案:

答案 0 :(得分:0)

您可以在实体

上使用AsParallel()来尝试PLINQ
var productbacklogworkitem =
               (from w in workItemcollectionList.AsParallel() 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.AsParallel() 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();

有关详细信息,请参阅:https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/introduction-to-plinq

答案 1 :(得分:0)

您可以使用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;