Dynamics CRM Linq Query Error
From Logic Wiki
“The method ‘Count’ is not supported”
My initial query as below, gave me the exception saying “The method ‘Count’ is not supported”
var xrm = newXrmServiceContext(“Xrm”);
var exampleContacts = xrm.ContactSet.Where(c => c.EMailAddress1.EndsWith(“.com”)).ToList();
var count = exampleContacts.Count<Contact>();
if (count > 0)
{
ContactsGridView.DataSource = exampleContacts;
ContactsGridView.DataBind();
}
It is due to the reason that by using var, .Net implicitly takes the type of the result and in this case it is
Microsoft.Xrm.Sdk.Linq.Query<ContactxampleContacts;
All I had to do was implicitly cast it to a list by using “ToList()” which helped me in getting the desired result back.
IEnumerable<Contact> exampleContacts = xrm.ContactSet.Where(c => c.EMailAddress1.EndsWith(“.com”)).ToList();
var count = exampleContacts.Count<Contact>();
if (count > 0)
{
string alert = String.Format(“<SCRIPT LANGUAGE=\”\”JavaScript\”\”>alert(\”Hello this is an Alert {0}\”)</SCRIPT>”, count);
System.Web.HttpContext.Current.Response.Write(alert);
ContactsGridView.DataSource = exampleContacts;
ContactsGridView.DataBind();
}