Exemple #1
0
        public static bool Contains(this object value, Microsoft.Xrm.Sdk.DataCollection <object> values)
        {
            if (values == null || values.Count != 1)
            {
                throw new ArgumentException("Values must contains exact one values");
            }
            if (value == null)
            {
                return(false);
            }

            var s = value as string;

            if (s == null)
            {
                throw new ArgumentException("Contains is only supported for fields of type string");
            }

            var containsValue = values.Single() as string;

            if (containsValue == null)
            {
                throw new ArgumentException("expected values to contain a single string");
            }

            return(s.ToUpperInvariant().IndexOf(containsValue.ToUpperInvariant()) >= 0);
        }
Exemple #2
0
        public static bool Equal(this object value, Microsoft.Xrm.Sdk.DataCollection <object> values)
        {
            if (values == null || values.Count < 1)
            {
                throw new ArgumentException("Equal expected at least one value in values");
            }

            if (value == null)
            {
                return(false);
            }

            var valueType = value.GetType();

            if (valueType == typeof(Guid) || valueType == typeof(Guid?))
            {
                var r = (Guid)value;
                var o = values.Single();
                if (o.GetType() == typeof(Guid))
                {
                    var g = (Guid)o;
                    return(r == g);
                }
            }

            if (valueType == typeof(Microsoft.Xrm.Sdk.EntityReference))
            {
                var r = (Microsoft.Xrm.Sdk.EntityReference)value;
                var o = values.Single();
                if (o.GetType() == typeof(Guid))
                {
                    var g = (Guid)o;
                    return(r.Id == g);
                }

                if (o.GetType() == typeof(Microsoft.Xrm.Sdk.EntityReference))
                {
                    var g = (Microsoft.Xrm.Sdk.EntityReference)o;
                    return(r.LogicalName == g.LogicalName && r.Id == g.Id);
                }
                throw new ArgumentException($"{o.GetType().FullName} was not expected for a reference id compare");
            }

            if (valueType == typeof(Microsoft.Xrm.Sdk.OptionSetValue))
            {
                var r = (Microsoft.Xrm.Sdk.OptionSetValue)value;
                var o = values.Single();
                if (o.GetType() == typeof(int) || o.GetType() == typeof(int?))
                {
                    var i = (int)o;
                    return(r.Value == i);
                }
                throw new ArgumentException($"{o.GetType().FullName} was not expected for a optionset compare");
            }

            if (valueType == typeof(Microsoft.Xrm.Sdk.OptionSetValueCollection))
            {
                var r = (Microsoft.Xrm.Sdk.OptionSetValueCollection)value;
                if (r.Count != values.Count)
                {
                    return(false);
                }

                var hasValues = r.Select(v => v.Value).ToArray();
                foreach (int i in values)
                {
                    if (!hasValues.Contains(i))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            if (valueType == typeof(Microsoft.Xrm.Sdk.Money))
            {
                var r = (Microsoft.Xrm.Sdk.Money)value;
                var o = values.Single();
                if (o.GetType() == typeof(decimal) || o.GetType() == typeof(decimal?))
                {
                    var i = (decimal)o;
                    return(r.Value == i);
                }
                throw new ArgumentException($"{o.GetType().FullName} was not expected for a Money compare");
            }

            if (valueType == typeof(int) || valueType == typeof(int?))
            {
                var r = (int)value;
                var o = values.Single();
                if (o.GetType() == typeof(int) || o.GetType() == typeof(int?))
                {
                    var i = (int)o;
                    return(r == i);
                }
                throw new ArgumentException($"{o.GetType().FullName} was not expected for an int compare");
            }

            if (valueType == typeof(decimal) || valueType == typeof(decimal?))
            {
                var r = (decimal)value;
                var o = values.Single();
                if (o.GetType() == typeof(decimal) || o.GetType() == typeof(decimal?))
                {
                    var i = (decimal)o;
                    return(r == i);
                }
                throw new ArgumentException($"{o.GetType().FullName} was not expected for an decimal compare");
            }

            if (valueType == typeof(double) || valueType == typeof(double?))
            {
                var r = (double)value;
                var o = values.Single();
                if (o.GetType() == typeof(double) || o.GetType() == typeof(double?))
                {
                    var i = (double)o;
                    return(r == i);
                }
                throw new ArgumentException($"{o.GetType().FullName} was not expected for an double compare");
            }

            if (valueType == typeof(float) || valueType == typeof(float?))
            {
                var r = (float)value;
                var o = values.Single();
                if (o.GetType() == typeof(float) || o.GetType() == typeof(float?))
                {
                    var i = (float)o;
                    return(r == i);
                }
                throw new ArgumentException($"{o.GetType().FullName} was not expected for an float compare");
            }

            if (valueType == typeof(bool) || valueType == typeof(bool?))
            {
                var r = (bool)value;
                var o = values.Single();
                if (o.GetType() == typeof(bool) || o.GetType() == typeof(bool?))
                {
                    var i = (bool)o;
                    return(r == i);
                }
                throw new ArgumentException($"{o.GetType().FullName} was not expected for an bool compare");
            }

            if (valueType == typeof(string))
            {
                var r = (string)value;
                var o = values.Single();
                if (o.GetType() == typeof(string))
                {
                    var s = (string)o;
                    return(r.ToUpperInvariant() == s.ToUpperInvariant());
                }
            }

            return(false);
        }