Linq and Parallel.ForEach loop

If requests are internal then Linq query will perform better over Parallel.ForEach and for external request Parallel.ForEach will perform better than Linq request.

Parller Foreach is faster than traditional For or ForEach loop

private List GetOrganizationRequests(List incidents)
         {
             var orgRequests = new List();
         Parallel.ForEach(incidents.Where(x => x.Contains("statuscode") && x.Contains("qi.queueitemid") && x.Contains("tm.queueid")), incident =>
         {
             orgRequests.Add(new AddToQueueRequest
             {
                 SourceQueueId = ((EntityReference)((AliasedValue)incident["qi.queueid"]).Value).Id,
                 Target = new EntityReference("incident", incident.Id),
                 DestinationQueueId = ((EntityReference)((AliasedValue)incident["tm.queueid"]).Value).Id
             });
         });
         return orgRequests;
     }

Linq query for same request

private List GetOrganizationRequests(List incidents) =>
             incidents.Where(x => x.Contains("statuscode") && x.Contains("qi.queueitemid") && x.Contains("tm.queueid"))
             .Select(z => new AddToQueueRequest()
             {
                 SourceQueueId = ((EntityReference)((AliasedValue)z["qi.queueid"]).Value).Id,
                 Target = new EntityReference("incident", z.Id),
                 DestinationQueueId = ((EntityReference)((AliasedValue)z["tm.queueid"]).Value).Id
             }).Cast().ToList();

hope its helps !!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s