public static object Convert(object value, Dynamics365Entity entity, string attributeLogicalName, Dynamics365Connection connection, string targetEntityLogicalName)
        {
            object                   convertedValue    = default(object);
            EntityMetadata           entityMetadata    = entity.GetEntityMetadata(connection);
            AttributeMetadata        attributeMetadata = entityMetadata.Attributes.FirstOrDefault(findField => findField.LogicalName == attributeLogicalName);
            Dynamics365TypeConverter converter         = new Dynamics365TypeConverter(attributeMetadata, targetEntityLogicalName);
            Type destinationType = converter.Dynamics365Type;

            if (converter.CanConvertTo(destinationType))
            {
                try
                {
                    convertedValue = converter.ConvertTo(value, destinationType);
                }
                catch (Exception ex)
                {
                    throw new ArgumentException(string.Format("Cannot convert {0} '{1}' to {2}: {3}", attributeMetadata.DisplayName.UserLocalizedLabel.Label, value, destinationType.Name, ex.Message), ex);
                }
            }
            else
            {
                throw new ArgumentException(string.Format("Cannot convert {0} '{1}' to {2}.", attributeMetadata.DisplayName.UserLocalizedLabel.Label, value, destinationType.Name));
            }

            return(convertedValue);
        }
        /// <summary>
        /// Gets a list of fields in an entity or link-entity node of a FetchXML query.
        /// </summary>
        /// <param name="entityNode">The entity node.</param>
        /// <returns>The fields.</returns>
        private List <Dynamics365Field> GetEntityNodeFields(XmlNode entityNode)
        {
            List <Dynamics365Field> fields         = new List <Dynamics365Field>();
            Dynamics365Entity       entity         = Dynamics365Entity.Create(entityNode.Attributes["name"].Value, (Dynamics365Connection)Parent);
            EntityMetadata          entityMetadata = entity.GetEntityMetadata((Dynamics365Connection)Parent);

            foreach (XmlNode node in entityNode.ChildNodes)
            {
                if (node.Name.ToLower() == "attribute")
                {
                    string            attributeName     = node.Attributes["name"].Value;
                    AttributeMetadata attributeMetadata = entityMetadata.Attributes.FirstOrDefault(findField => findField.LogicalName == attributeName);

                    if (attributeMetadata != default(AttributeMetadata))
                    {
                        fields.Add(Dynamics365Field.CreateFromMetadata(attributeMetadata, (Dynamics365Connection)Parent));
                    }
                    else
                    {
                        throw new ApplicationException(string.Format("The {0} field in {1} does not exist in Dynamics 365", attributeName, Name));
                    }
                }

                if (node.Name.ToLower() == "link-entity")
                {
                    List <Dynamics365Field> linkedFields = GetEntityNodeFields(node);

                    foreach (Dynamics365Field linkedField in linkedFields)
                    {
                        fields.Add(linkedField);
                    }
                }
            }

            return(fields);
        }