Beispiel #1
0
        /// <summary>
        /// Checks the entity sort field.
        /// </summary>
        /// <typeparam name="TEntity">The type of the t entity.</typeparam>
        /// <param name="qb">The qb.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        /// <exception cref="System.Exception">$the TEntity has not {qb.SortField} property</exception>
        public static bool CheckEntitySortField <TEntity>(this BaseQueryBuilder qb)
        {
            PropertyInfo sortProperty = typeof(TEntity).GetProperty(qb.SortField, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);

            if (sortProperty == null)
            {
                throw new Exception($"the TEntity has not SortField value's property");
            }

            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Checks the entity sort order.
        /// </summary>
        /// <param name="qb">The qb.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        /// <exception cref="System.Exception">$the TEntity has not {qb.SortField} property</exception>
        public static bool CheckEntitySortOrder(this BaseQueryBuilder qb)
        {
            List <string> orderType = new List <string> {
                "asc", "desc"
            };

            if (orderType.Contains(qb.SortOrder.ToLower()))
            {
                return(true);
            }

            throw new Exception("please set SortOrder value(asc or desc)");
        }
Beispiel #3
0
        /// <summary>
        /// Checks the entity field.
        /// </summary>
        /// <typeparam name="TEntity">The type of the t entity.</typeparam>
        /// <param name="qb">The qb.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        /// <exception cref="System.Exception">$the object dose not have {string.Join(;,, noneFields)} fields</exception>
        public static bool CheckEntityField <TEntity>(this BaseQueryBuilder qb)
        {
            if (qb.Items.Count() != 0)
            {
                var fields       = qb.Items.Select(p => p.Field).ToList();
                var entityFields = typeof(TEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase).Select(p => p.Name);
                var noneFields   = fields.FindAll(p => !entityFields.Contains(p));
                if (noneFields.Count() != 0)
                {
                    throw new Exception($"the object dose not have {string.Join(",", noneFields)} fields");
                }
            }

            return(true);
        }
Beispiel #4
0
        /// <summary>
        ///     Wheres the specified qb.
        /// </summary>
        /// <typeparam name="TEntity">The type of the t entity.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="qb">The qb.</param>
        /// <returns>IQueryable&lt;TEntity&gt;.</returns>
        /// <exception cref="System.Exception">Please Set Default Sort Field</exception>
        public static IQueryable <TEntity> Where <TEntity>(this IQueryable <TEntity> source, BaseQueryBuilder qb)
        {
            qb.CheckEntityField <TEntity>();


            var query = source.Where((SearchCondition)qb);

            //if (qb.DefaultSort && typeof(TEntity).GetProperty(qb.SortField, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance) == null)
            //{
            //    qb.SortField = "SortIndex";
            //    qb.SortOrder = "asc";
            //}

            qb.TotolCount = query.Count();

            if (!string.IsNullOrEmpty(qb.SortField) && qb.DefaultSort)
            {
                query = query.OrderBy(qb.SortField, string.Equals(qb.SortOrder, SortMode.Asc.ToString(), StringComparison.CurrentCultureIgnoreCase));
            }
            else if (qb.DefaultSort)
            {
                throw new Exception("Please Set Default Sort Field");
                //query = query.OrderBy<TEntity>("ID", false);
            }

            if (qb.PageSize == 0)
            {
                return(query);
            }
            if (qb.NeedPaging)
            {
                query = query.Where(qb.PageSize, qb.PageIndex);
            }


            return(query);
        }