/// <summary>
        /// Gets the entities count.
        /// </summary>
        /// <param name="logicalName">Name of the logical.</param>
        /// <param name="primaryKey">The primary key.</param>
        /// <param name="filteredFields">The filtered fields.</param>
        /// <param name="onlyActive">if set to <c>true</c> [only active].</param>
        /// <returns></returns>
        public override int GetEntitiesCount(string logicalName, string primaryKey, Dictionary <string, string> filteredFields, bool onlyActive)
        {
            const string CountQuery = "<fetch mapping='logical' aggregate='true'><entity name='{0}'><attribute name='{1}' aggregate='count' alias='count' />{2}</entity></fetch>";
            var          xml        = this.crmService.Fetch(CountQuery.FormatWith(logicalName, primaryKey, this.GetFilterExpression(filteredFields, onlyActive).ToFetchXml()));

            var resultXml  = XElement.Parse(xml);
            var resultNode = resultXml.Element("result");

            int returnValue = 0;

            if (resultNode == null)
            {
                return(returnValue);
            }

            var node = resultNode.Element("count");

            if (node == null)
            {
                return(returnValue);
            }

            int.TryParse(node.Value, out returnValue);

            return(returnValue);
        }
        /// <summary>
        /// Gets the entities count.
        /// </summary>
        /// <param name="entityName">The entity name.</param>
        /// <param name="primaryKey">The primary key.</param>
        /// <param name="expression">The filter expression.</param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"><c>InvalidOperationException</c>.</exception>
        public override int GetEntitiesCount(string entityName, string primaryKey, FilterExpression expression)
        {
            Assert.ArgumentNotNullOrEmpty(entityName, "entityName");
            Assert.ArgumentNotNullOrEmpty(primaryKey, "primaryKey");

            string xml =
                CrmRemoteSettings.CrmService.Fetch(CountQuery.FormatWith(entityName, primaryKey,
                                                                         expression != null
                                                                  ? expression.ToFetchXml()
                                                                  : string.Empty));

            var resultXml  = XElement.Parse(xml);
            var resultNode = resultXml.Element("result");

            if (resultNode == null)
            {
                throw new InvalidOperationException(ResourceManager.Localize("CRM_AGR_QUERY_ERROR"));
            }
            var node = resultNode.Element("count");

            try
            {
                return(int.Parse(node.Value));
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(ResourceManager.Localize("CRM_AGR_QUERY_ERROR"), ex);
            }
        }