[In addition to blogging, I am also now using Twitter for quick updates and to share links. Follow me at: twitter.com/pabloperalta] TIP: How to easily workaround “The method 'Count' is not supported” - Pablo Peralta's Blog

TIP: How to easily workaround “The method 'Count' is not supported”

Hi,

I came across this problem when running a LINQ query through Dynamics CRM LINQ provider for getting the count of records returned by my query.

My query looked like this:

 

var lineItems = from lineItem in MyXrmServiceContext.OpportunityProductSet
                         where lineItem.OpportunityId.Id == opportunityId
                         select lineItem;
 
 if (lineItems != null && lineItems.Count() > 0)
 {
     foreach (OpportunityProduct lineItem in lineItemsToDelete)
     {
         //do something
     }
 }

 

Unfortunately this simple statement (lineItems.Count()) throws the following exception:

 

The method ‘Count’ is not supported

 

I have made some research and found some ways to workaround it by using FetchXml or RetrieveMultiple to get the count of records. Nevertheless, I discovered another workaround much simpler I guess that could perfectly work in case you are working with few records (I think it may be not so performant with relevant amount of records). The approach is just converting the IQueryable collection to a List, as shown below:

 

var lineItems = from lineItem in MyXrmServiceContext.OpportunityProductSet
                         where lineItem.OpportunityId.Id == opportunityId
                         select lineItem;
 
 if (lineItems != null && lineItems.ToList<OpportunityProduct>().Count() > 0)
 {
     foreach (OpportunityProduct lineItem in lineItemsToDelete)
     {
         //do something
     }
 }

 

Now, getting the count of records works like a charm for me Smile.

 

Hope it helps you in your developments,

 

PP [twitter: @pabloperalta]

UruIT Dynamix | Excellence in Dynamics CRM Nearshoring Services.

uruit_dynamix_016[2]

Published Monday, May 21, 2012 1:55 AM by pablop

Comments

# TIP: How to easily workaround ???The method 'Count' is not supported??? - Pablo Peralta's Blog

Pingback from  TIP: How to easily workaround ???The method 'Count' is not supported??? - Pablo Peralta's Blog

# re: TIP: How to easily workaround “The method 'Count' is not supported”

Thursday, October 25, 2012 6:11 AM by Ramon

Thanks Pablo, I was just having the same issue in a simple plugin :)

# re: TIP: How to easily workaround “The method 'Count' is not supported”

Sunday, March 03, 2013 11:22 PM by Pratik

Can you tell me why Dynamic CRM does not support Group by Clause using LINQ?

# re: TIP: How to easily workaround “The method 'Count' is not supported”

Thursday, April 18, 2013 10:11 AM by Tony

Calling ToList() will retrieve the entire data in memory surely?!?

# re: TIP: How to easily workaround “The method 'Count' is not supported”

Thursday, April 18, 2013 11:08 AM by pablop

Hi Tony,

AFAIK, yes.

Let me know if worked for you, regards,

PP

Leave a Comment

(required) 
(required) 
(optional)
(required)