コード例 #1
0
 /// <summary>
 /// 如果提供的值是不可空的,则为查询添加一个对应的约束条件,并以 And 与原条件进行连接。
 /// </summary>
 /// <param name="query">查询.</param>
 /// <param name="property">要约束的属性.</param>
 /// <param name="op">约束条件操作符.</param>
 /// <param name="value">当 value 不可空时,才添加这个对比约束条件。</param>
 /// <returns></returns>
 public static IQuery AddConstraintIf(this IQuery query, IManagedProperty property, PropertyOperator op, object value)
 {
     if (ConditionalSql.IsNotEmpty(value))
     {
         return(AddConstraint(query, property, op, value));
     }
     return(query);
 }
コード例 #2
0
        /// <summary>
        /// 根据传入的属性列表,来构造 CommonQueryCriteria
        /// 返回是否有非空属性需要验证。
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="criteria"></param>
        /// <returns></returns>
        private bool AddToCriteria(Entity entity, CommonQueryCriteria criteria)
        {
            bool hasValue = false;

            foreach (IProperty property in this.Properties)
            {
                EnsurePropertyCategory(property);

                var value = entity.GetProperty(property);
                if (ConditionalSql.IsNotEmpty(value))
                {
                    hasValue = true;
                    criteria.Add(property, value);
                }
            }
            if (entity.HasId)
            {
                criteria.Add(Entity.IdProperty, PropertyOperator.NotEqual, entity.Id);
            }
            return(hasValue);
        }
コード例 #3
0
        /// <summary>
        /// <see cref="CommonQueryCriteria"/> 查询的数据层实现。
        /// </summary>
        /// <param name="criteria"></param>
        public virtual EntityList GetBy(CommonQueryCriteria criteria)
        {
            var table = qf.Table(_repository);
            var q     = qf.Query(table);

            var allProperties = _repository.EntityMeta.ManagedProperties.GetNonReadOnlyCompiledProperties();

            //拼装所有 Where 条件。
            bool ignoreNull = criteria.IgnoreNull;

            foreach (var group in criteria.Groups)
            {
                IConstraint groupRes = null;
                foreach (var pm in group)
                {
                    var property = allProperties.Find(pm.PropertyName);
                    if (property != null)
                    {
                        var  op      = pm.Operator;
                        var  value   = pm.Value;
                        bool ignored = false;
                        if (ignoreNull)
                        {
                            ignored = !ConditionalSql.IsNotEmpty(value);
                        }
                        else
                        {
                            if (value is string || (value == null && property.PropertyType == typeof(string)))
                            {
                                #region 如果是对空字符串进行模糊匹配,那么这个条件需要被忽略。

                                var strValue = value as string;
                                if (string.IsNullOrEmpty(strValue))
                                {
                                    switch (op)
                                    {
                                    case PropertyOperator.Like:
                                    case PropertyOperator.Contains:
                                    case PropertyOperator.StartWith:
                                    case PropertyOperator.EndWith:
                                        ignored = true;
                                        break;

                                    default:
                                        break;
                                    }
                                }

                                #endregion
                            }
                        }
                        if (!ignored)
                        {
                            var propertyRes = qf.Constraint(table.Column(property), op, value);
                            groupRes = qf.Binary(groupRes, group.Concat, propertyRes);
                        }
                    }
                }

                q.Where = qf.Binary(groupRes, criteria.Concat, q.Where);
            }

            //OrderBy
            if (!string.IsNullOrWhiteSpace(criteria.OrderBy))
            {
                var orderBy = allProperties.Find(criteria.OrderBy);
                if (orderBy != null)
                {
                    var dir = criteria.OrderByAscending ? OrderDirection.Ascending : OrderDirection.Descending;
                    q.OrderBy.Add(table.Column(orderBy), dir);
                }
            }

            return(this.QueryList(q, criteria.PagingInfo, criteria.EagerLoad));
        }