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

问题描述 投票:0回答:2

我有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();
c# multithreading linq tfs-workitem plinq
2个回答
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


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;
© www.soinside.com 2019 - 2024. All rights reserved.