Grouping in LINQ in ms crm early bound classes

Yesterday i faced a requirement where i have to group work orders in crm on the basis of customer.

Below is how to do it:

//Get In progress Work orders

List<msdyn_workorder> WorkOrderList = new List<msdyn_workorder>(orgContext.CreateQuery<msdyn_workorder>().Where(i => i.msdyn_systemstatus.value == 690970002)); 

 var obj = from _WO in WorkOrderList   
group _WO by _WO.msdyn_serviceaccount.Id into CUSTOMER                      
     select new                        
   {               
      CUSTOMER.Key,                               
      Count = CUSTOMER.Count()                
   };       
Int32 count = Enumerable.Count(obj3); 
foreach(var iobj in obj) 
{ 
//iobj have Customer Id and count of work orders as below 
//Vishal 2 //Abhishek 3
 }

Here most important line to understand is

List<msdyn_workorder> WorkOrderList = new List<msdyn_workorder>(orgContext.CreateQuery<msdyn_workorder>().Where(i => i.msdyn_systemstatus.value == 690970002));

If we don’t cast the context entity result into List of type <msdyn_workorder> first and apply grouping directly, we’ll get the exception as “The method Groupby is not supported”.

Probably the error occurred because grouping doesn’t work on anonymous type collection which we were getting.

Hope it would be helpful.

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 )

Facebook photo

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

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: