LINQ: GroupBy, Sum and EntityCollection

Just resharing a sample code that could be used for group by and sum operation on EntityCollection.

Suppose below is the output that we want

Continue reading “LINQ: GroupBy, Sum and EntityCollection”

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 !!

Linq query on EntityCollection

linq

Querying SQL database all the time may affect performance so some time we can pull the generic data and then query on that result collection may give better result.

Here is the example of it,

Continue reading “Linq query on EntityCollection”