Exemplo n.º 1
0
    static bool BuildObjectCompare(Comparison comparison, string value, PropertyAccessor propertyFunc, T target)
    {
        var propertyValue = propertyFunc.Func(target);
        var typedValue    = TypeConverter.ConvertStringToType(value, propertyFunc.Type);

        var compare = Compare(propertyValue, typedValue);

        switch (comparison)
        {
        case Comparison.Equal:
            return(compare == 0);

        case Comparison.NotEqual:
            return(compare != 0);

        case Comparison.GreaterThan:
            return(compare > 0);

        case Comparison.GreaterThanOrEqual:
            return(compare >= 0);

        case Comparison.LessThanOrEqual:
            return(compare <= 0);

        case Comparison.LessThan:
            return(compare < 0);
        }

        throw new NotSupportedException($"Invalid comparison operator '{comparison}'.");
    }
Exemplo n.º 2
0
 static Func <T, bool> BuildStringIn(PropertyAccessor property, string[] values, StringComparison stringComparison)
 {
     return(target =>
     {
         var propertyValue = (string)property.Func(target);
         return values.Any(x => string.Equals(x, propertyValue, stringComparison));
     });
 }
Exemplo n.º 3
0
    static bool BuildStringCompare(Comparison comparison, string value, PropertyAccessor propertyFunc, T x, StringComparison stringComparison)
    {
        var propertyValue = (string)propertyFunc.Func(x);

        switch (comparison)
        {
        case Comparison.Equal:
            return(string.Equals(propertyValue, value, stringComparison));

        case Comparison.NotEqual:
            return(!string.Equals(propertyValue, value, stringComparison));

        case Comparison.Contains:
            return(propertyValue?.IndexOf(value, stringComparison) >= 0);

        case Comparison.StartsWith:
            return(propertyValue != null && propertyValue.StartsWith(value, stringComparison));

        case Comparison.EndsWith:
            return(propertyValue != null && propertyValue.EndsWith(value, stringComparison));
        }

        throw new NotSupportedException($"Invalid comparison operator '{comparison}'.");
    }
Exemplo n.º 4
0
    static Func <T, bool> BuildObjectIn(PropertyAccessor property, string[] values)
    {
        return(target =>
        {
            var type = property.Type;
            var propertyValue = property.Func(target);

            if (type == typeof(Guid))
            {
                var value = (Guid)propertyValue;
                return values.Select(Guid.Parse)
                .Any(x => x == value);
            }

            if (type == typeof(int))
            {
                var value = (int)propertyValue;
                return values.Select(int.Parse)
                .Any(x => x == value);
            }

            if (type == typeof(short))
            {
                var value = (short)propertyValue;
                return values.Select(short.Parse)
                .Any(x => x == value);
            }

            if (type == typeof(long))
            {
                var value = (long)propertyValue;
                return values.Select(long.Parse)
                .Any(x => x == value);
            }

            if (type == typeof(uint))
            {
                var value = (uint)propertyValue;
                return values.Select(uint.Parse)
                .Any(x => x == value);
            }

            if (type == typeof(ushort))
            {
                var value = (ushort)propertyValue;
                return values.Select(ushort.Parse)
                .Any(x => x == value);
            }

            if (type == typeof(ulong))
            {
                var value = (ulong)propertyValue;
                return values.Select(ulong.Parse)
                .Any(x => x == value);
            }

            if (type == typeof(DateTime))
            {
                var value = (DateTime)propertyValue;
                return values.Select(DateTime.Parse)
                .Any(x => x == value);
            }

            if (type == typeof(DateTimeOffset))
            {
                var value = (DateTimeOffset)propertyValue;
                return values.Select(DateTimeOffset.Parse)
                .Any(x => x == value);
            }

            throw new Exception($"Could not convert strings to {type.FullName} ");
        });
    }