private bool WriteGroup(EntityFilter entityFilter)
        {
            // check for group start
            var hasGroup = entityFilter.Filters != null && entityFilter.Filters.Any();

            if (!hasGroup)
            {
                return(false);
            }

            var logic = string.IsNullOrWhiteSpace(entityFilter.Logic)
                ? EntityFilterLogic.And
                : entityFilter.Logic;

            var wroteFirst = false;

            _expression.Append("(");
            foreach (var filter in entityFilter.Filters)
            {
                if (wroteFirst)
                {
                    _expression.Append($" {logic} ");
                }

                Visit(filter);
                wroteFirst = true;
            }
            _expression.Append(")");

            return(true);
        }
        private void Visit(EntityFilter entityFilter)
        {
            if (WriteGroup(entityFilter))
            {
                return;
            }

            WriteExpression(entityFilter);
        }
        /// <summary>
        /// Builds a string base Linq expression from the specified <see cref="EntityFilter"/>.
        /// </summary>
        /// <param name="entityFilter">The entity filter to build expression from.</param>
        public void Build(EntityFilter entityFilter)
        {
            _expression.Length = 0;
            _values.Clear();

            if (entityFilter == null)
            {
                return;
            }

            Visit(entityFilter);
        }
        private void WriteExpression(EntityFilter entityFilter)
        {
            // name require for expression
            if (string.IsNullOrWhiteSpace(entityFilter.Name))
            {
                return;
            }

            // parameter index number
            int index = _values.Count;

            var name  = entityFilter.Name;
            var value = entityFilter.Value;

            // translate operator
            var o = string.IsNullOrWhiteSpace(entityFilter.Operator) ? "==" : entityFilter.Operator;

            _operatorMap.TryGetValue(o, out string comparison);
            if (string.IsNullOrEmpty(comparison))
            {
                comparison = o.Trim();
            }

            // use function call
            var negation = comparison.StartsWith("!") || comparison.StartsWith("not", StringComparison.OrdinalIgnoreCase) ? "!" : string.Empty;

            if (comparison.EndsWith(EntityFilterOperators.StartsWith, StringComparison.OrdinalIgnoreCase))
            {
                _expression.Append($"{negation}{name}.StartsWith(@{index})");
            }
            else if (comparison.EndsWith(EntityFilterOperators.EndsWith, StringComparison.OrdinalIgnoreCase))
            {
                _expression.Append($"{negation}{name}.EndsWith(@{index})");
            }
            else if (comparison.EndsWith(EntityFilterOperators.Contains, StringComparison.OrdinalIgnoreCase))
            {
                _expression.Append($"{negation}{name}.Contains(@{index})");
            }
            else
            {
                _expression.Append($"{name} {comparison} @{index}");
            }

            _values.Add(value);
        }
 public EntitySelect(EntityFilter filter, IEnumerable <EntitySort> sort)
 {
     Filter = filter;
     Sort   = sort;
 }
 public EntitySelect(EntityFilter filter)
     : this(filter, null)
 {
 }
 public EntitySelectQuery(IPrincipal principal, EntityFilter filter, IEnumerable <EntitySort> sort)
     : base(principal)
 {
     Filter = filter;
     Sort   = sort;
 }
 public EntitySelectQuery(IPrincipal principal, EntityFilter filter, EntitySort sort)
     : this(principal, filter, new[] { sort })
 {
 }
 public EntitySelectQuery(IPrincipal principal, EntityFilter filter)
     : this(principal, filter, (IEnumerable <EntitySort>)null)
 {
 }
Пример #10
0
 public EntitySelectQuery(IPrincipal principal, EntityFilter filter, IEnumerable <EntitySort> sort)
     : this(principal, new EntitySelect(filter, sort))
 {
 }
Пример #11
0
 public EntitySelectQuery(IPrincipal principal, EntityFilter filter)
     : this(principal, new EntitySelect(filter))
 {
 }