/// <summary> /// 返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queryable"></param> /// <param name="expression"></param> /// <returns></returns> public static bool Any <T>(this Queryable <T> queryable, Expression <Func <T, bool> > expression) { var type = queryable.Type; queryable.WhereIndex = queryable.WhereIndex + 100; ResolveExpress re = new ResolveExpress(queryable.WhereIndex); re.ResolveExpression(re, expression); queryable.Where.Add(re.SqlWhere); queryable.Params.AddRange(re.Paras); return(queryable.Count() > 0); }
/// <summary> /// 专门用于分页控件 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="allItems"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> public static PagedList <T> ToPagedList <T>(this Queryable <T> allItems, int pageIndex, int pageSize) { if (pageIndex < 1) { pageIndex = 1; } var itemIndex = (pageIndex - 1) * pageSize; var totalItemCount = allItems.Count(); while (totalItemCount <= itemIndex && pageIndex > 1) { itemIndex = (--pageIndex - 1) * pageSize; } var pageOfItems = allItems.Skip(itemIndex).Take(pageSize).ToList(); return(new PagedList <T>(pageOfItems, pageIndex, pageSize, totalItemCount)); }
/// <summary> /// 确定序列是否包含任何元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queryable"></param> /// <returns></returns> public static bool Any <T>(this Queryable <T> queryable) { return(queryable.Count() > 0); }