/// <inheritdoc /> protected override void Execute(Context context) { var query = FetchXml.Get(context); var result = context.Service.RetrieveMultiple(query); if (result.Entities.Count < 1) { return; } decimal resultAsDecimal = 0; foreach (var entity in result.Entities) { foreach (var attribute in entity.Attributes) { if (attribute.Value is decimal || attribute.Value is double || attribute.Value is int) { resultAsDecimal += Convert.ToDecimal(attribute.Value); } else if (attribute.Value is Money) { resultAsDecimal += ((Money)attribute.Value).Value; } } } ResultAsDecimal.Set(context, resultAsDecimal); ResultAsDouble.Set(context, Convert.ToDouble(resultAsDecimal)); ResultAsInteger.Set(context, Convert.ToInt32(resultAsDecimal)); ResultAsMoney.Set(context, new Money(resultAsDecimal)); }
/// <inheritdoc /> protected override void Execute(Context context) { var query = FetchXml.Get(context); if (string.IsNullOrWhiteSpace(query)) { return; } var result = context.Service.RetrieveMultiple(query); if (result.Entities.Count < 1) { return; } var values = new List <decimal>(); foreach (var entity in result.Entities) { foreach (var attribute in entity.Attributes) { if (attribute.Value is decimal || attribute.Value is double || attribute.Value is int) { values.Add(Convert.ToDecimal(attribute.Value)); } else if (attribute.Value is Money) { values.Add(((Money)attribute.Value).Value); } } } if (values.Count == 0) { return; } var sortedValues = values.OrderBy(number => number); var itemIndex = sortedValues.Count() / 2; var resultAsDecimal = sortedValues.Count() % 2 == 0 ? (sortedValues.ElementAt(itemIndex) + sortedValues.ElementAt(itemIndex - 1)) / 2 : sortedValues.ElementAt(itemIndex); ResultAsDecimal.Set(context, resultAsDecimal); ResultAsDouble.Set(context, Convert.ToDouble(resultAsDecimal)); ResultAsInteger.Set(context, Convert.ToInt32(resultAsDecimal)); ResultAsMoney.Set(context, new Money(resultAsDecimal)); }
/// <inheritdoc /> protected override void Execute(Context context) { var query = FetchXml.Get(context); if (string.IsNullOrWhiteSpace(query)) { return; } var result = context.Service.RetrieveMultiple(query); if (result.Entities.Count < 1) { return; } var entity = result.Entities.First(); var regex = new Regex(@"<attribute name=[""'](?<name>[^\s]+)[""']\s*/>", RegexOptions.IgnoreCase | RegexOptions.Multiline); var attributeName = regex.Matches(query).Cast <Match>().Select(match => match.Groups["name"].Value).FirstOrDefault(); if (attributeName == null) { ResultAsString.Set(context, entity.Id.ToString()); return; } if (entity.Attributes.Keys.All(a => a != attributeName)) { attributeName = entity.Attributes.Keys.FirstOrDefault(a => a.EndsWith($".{attributeName}")); } if (!entity.Contains(attributeName)) { return; } var attributeValue = entity.Attributes[attributeName]; if (attributeValue is AliasedValue) { attributeValue = ((AliasedValue)attributeValue).Value; } if (attributeValue is Money) { attributeValue = ((Money)attributeValue).Value; } if (attributeValue is EntityReference) { attributeValue = ((EntityReference)attributeValue).Id; } ResultAsString.Set(context, attributeValue.ToString()); if (attributeValue is DateTime) { ResultAsDateTime.Set(context, (DateTime)attributeValue); return; } if (attributeValue is OptionSetValue) { attributeValue = ((OptionSetValue)attributeValue).Value; // TODO: Получение метки элемента списка и отображение его в качестве текстового значения. ResultAsString.Set(context, attributeValue.ToString()); } if (attributeValue is bool || attributeValue is decimal || attributeValue is double || attributeValue is int) { var decimalValue = Convert.ToDecimal(attributeValue); ResultAsBoolean.Set(context, Convert.ToBoolean(attributeValue)); ResultAsDecimal.Set(context, decimalValue); ResultAsDouble.Set(context, Convert.ToDouble(attributeValue)); ResultAsInteger.Set(context, Convert.ToInt32(attributeValue)); ResultAsMoney.Set(context, new Money(decimalValue)); } }