/// <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); }
/// <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)"); }
/// <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); }
/// <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<TEntity>.</returns> /// <exception cref="System.Exception">Please Set Default Sort Field</exception> public static IQueryable <TEntity> Where <TEntity>(this IQueryable <TEntity> source, BaseQueryBuilder qb) { var query = source.Where((SearchCondition)qb); if (qb.DefaultSort && typeof(TEntity).GetProperty("SortIndex") != null) { qb.SortField = "SortIndex"; qb.SortOrder = "asc"; } qb.TotolCount = query.Count(); if (!string.IsNullOrEmpty(qb.SortField)) { query = query.OrderBy(qb.SortField, string.Equals(qb.SortOrder, SortMode.Asc.ToString(), StringComparison.CurrentCultureIgnoreCase)); } else { throw new Exception("Please Set Default Sort Field"); //query = query.OrderBy<TEntity>("ID", false); } if (qb.PageSize == 0) { return(query); } query = query.Skip(qb.PageSize * qb.PageIndex).Take(qb.PageSize); return(query); }
/// <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<TEntity>.</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); }