Exemplo n.º 1
0
 public void Build(EntityFilter entityFilter)
 {
     _expression.Length = 0;
     _values.Clear();
     if (entityFilter == null)
     {
         return;
     }
     Visit(entityFilter);
 }
Exemplo n.º 2
0
        public static IQueryable <T> Filter <T>(this IQueryable <T> query, EntityFilter filter)
        {
            if (filter == null)
            {
                return(query);
            }

            var builder = new LinqExpressionBuilder();

            builder.Build(filter);
            var predicate  = builder.Expression;
            var parameters = builder.Parameters.ToArray();

            if (string.IsNullOrWhiteSpace(predicate))
            {
                return(query);
            }
            return(query.Where(predicate, parameters));
        }
Exemplo n.º 3
0
        private bool WriteGroup(EntityFilter entityFilter)
        {
            var hasGroup = entityFilter.Filters != null && entityFilter.Filters.Any();

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

            if (!string.IsNullOrWhiteSpace(entityFilter.Name))
            {
                var logic = string.IsNullOrWhiteSpace(entityFilter.Logic)
                    ? EntityFilterLogic.And
                    : entityFilter.Logic;
                _expression.Append($" {logic} (");
            }
            else
            {
                _expression.Append("(");
            }

            foreach (var filter in entityFilter.Filters)
            {
                Visit(filter);

                var isLast = entityFilter.Filters.LastOrDefault() == filter;
                if (filter == null)
                {
                    continue;
                }
                var filterLogic = string.IsNullOrWhiteSpace(filter.Logic)
                    ? EntityFilterLogic.And
                    : filter.Logic;
                if (!isLast)
                {
                    _expression.Append($" {filterLogic} ");
                }
            }
            _expression.Append(")");
            return(true);
        }
Exemplo n.º 4
0
        private void WriteExpression(EntityFilter entityFilter)
        {
            if (string.IsNullOrWhiteSpace(entityFilter.Name))
            {
                return;
            }
            var index = _values.Count;
            var name  = entityFilter.Name;
            var value = entityFilter.Value;
            var o     = string.IsNullOrWhiteSpace(entityFilter.Operator) ? "==" : entityFilter.Operator;

            _operatorMap.TryGetValue(o, out string comparison);
            if (string.IsNullOrEmpty(comparison))
            {
                comparison = o.Trim();
            }
            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);
        }
Exemplo n.º 5
0
        public EntityListQuery(EntityFilter filter, IEnumerable <EntitySort> sort)

        {
            Filter = filter;
            Sort   = sort;
        }
Exemplo n.º 6
0
 public EntityListQuery(EntityFilter filter, string includeProperties)
     : this(filter, (IEnumerable <EntitySort>)null)
 {
     this.IncludeProperties = includeProperties;
 }
Exemplo n.º 7
0
 public EntityListQuery(EntityFilter filter, EntitySort sort)
     : this(filter, new[] { sort })
 {
 }
Exemplo n.º 8
0
 public EntityListQuery(EntityFilter filter)
     : this(filter, (IEnumerable <EntitySort>)null)
 {
 }
Exemplo n.º 9
0
        private void Visit(EntityFilter entityFilter)
        {
            WriteExpression(entityFilter);

            WriteGroup(entityFilter);
        }