1

I have 2 Linq Queries, 1st Linq Query returns 1148 records and 2nd returns 6667 records. They take 8 minutes in execution. Is there any way to make them faster while running parallel?

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();
Chirag Jain
  • 628
  • 11
  • 24
Deepak Jain
  • 137
  • 1
  • 3
  • 27
  • `System.Threading.Tasks.Parallel.ForEach` search for this tune your code into the mentioned format. It would be useful for the question. – kvk30 Feb 27 '18 at 05:29
  • 1
    Why are you pulling ~7000 records from the database with a single query? – xxbbcc Feb 27 '18 at 05:39
  • 1
    just by chance 7k records are there @xxbbcc – Deepak Jain Feb 27 '18 at 06:01
  • @AkhilJain do you _need_ to pull all data in one go? Even in parallel it will not suddenly speed up massively, besides as your data grows it will likely just slow down more and more. Seems this is a good place to implement some paging, so you load only X rows each time from the database. – RMH Feb 27 '18 at 08:24
  • i am export the list in excel not any grid. @RMH pls tell the parallel way. – Deepak Jain Feb 27 '18 at 08:52
  • 1
    @AkhilJain could you show what the `workItemcollectionList` is and the class of the items within? You can omit stuff, as long you got a [mcve](https://stackoverflow.com/help/mcve) example of it. The answer of Anupam Singh seems good, but maybe fails due to some missing context. – RMH Feb 27 '18 at 09:34
  • Is this SQL query or `workItemcollectionList` just collection of items already in memory? – Alexey Klipilin Mar 04 '18 at 11:35
  • workcollectionlist – Deepak Jain Mar 05 '18 at 02:45

2 Answers2

2

You can use Task to make also two queries parallel.

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;
Alexey Klipilin
  • 1,866
  • 13
  • 29
0

You can try PLINQ, using AsParallel() on entity

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();

For more info see: https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/introduction-to-plinq

Anupam Singh
  • 1,158
  • 13
  • 25