Here it is : -
private void btnUpdate_Click(object sender, EventArgs e)
{
string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' >
<entity name='contact'>
<attribute name='fullname' />
<attribute name='ownerid' />
<attribute name='parentcustomerid' />
<attribute name='aac_palno' />
<attribute name='mobilephone' />
<attribute name='jobtitle' />
<attribute name='telephone1' />
<attribute name='address1_country' />
<attribute name='address1_city' />
<attribute name='emailaddress1' />
<attribute name='address1_stateorprovince' />
<attribute name='contactid' />
<order attribute='parentcustomerid' descending='false' />
<order attribute='fullname' descending='false' />
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0' />
</filter>
<link-entity name='account' from='accountid' to='parentcustomerid' visible='false' link-
type='outer' alias='a_******************5'>
<attribute name='accountnumber' />
<attribute name='aac_nearestairportid' />
</link-entity>
</entity>
</fetch>";
MessageBox.Show($"Count of Records:{totalCount.Entities.Count}");
}
public static int GetRecordCount(string fetchXml, string attributeName, ConditionExpression expression)
{
int rc = 0;
StringBuilder sb = new StringBuilder();
sb.AppendLine("<fetch distinct='false' mapping='logical' aggregate='true'> ");
sb.AppendLine(" <contact='" + fetchXml + "'> ");
sb.AppendLine(" <attribute name='" + attributeName + "' alias='" + attributeName + "_count' aggregate='count'/> ");
sb.AppendLine(" <filter type='and'>");
if (expression.Values.Count > 1)
{
sb.AppendLine(" <condition attribute='" + expression.AttributeName + "' operator='in'>");
for (int i = 0; i < expression.Values.Count; i++)
{
sb.AppendLine(" <value>" + expression.Values[i].ToString() + "</value>");
}
sb.AppendLine(" </condition>");
}
else
{
sb.AppendLine(" <condition attribute='" + expression.AttributeName + "' operator='eq' value='" + expression.Values[0].ToString() + "' /> ");
}
sb.AppendLine(" <condition attribute='statecode' operator='eq' value='0' /> ");
sb.AppendLine(" </filter> ");
sb.AppendLine(" </entity> ");
sb.AppendLine("</fetch>");
EntityCollection totalCount = _service.RetrieveMultiple(new FetchExpression(sb.ToString()));
if (totalCount.Entities.Count > 0)
{
foreach (var t in totalCount.Entities)
{
rc = (int)((AliasedValue)t[attributeName + "_count"]).Value;
break;
}
}
return rc;
}
}