/// <summary>
 ///  返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="queryable"></param>
 /// <returns></returns>
 public static T Single <T>(this Queryable <T> queryable)
 {
     if (queryable.OrderBy.IsNullOrEmpty())
     {
         queryable.OrderBy = "GETDATE()";
     }
     queryable.Skip(0);
     queryable.Take(1);
     return(queryable.ToList().Single());
 }
        /// <summary>
        ///  返回序列中的第一个元素。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <returns></returns>
        public static T First <T>(this Queryable <T> queryable)
        {
            if (queryable.OrderBy.IsNullOrEmpty())
            {
                queryable.OrderBy = "GETDATE()";
            }
            queryable.Skip(0);
            queryable.Take(1);
            var reval = queryable.ToList();

            return(reval.First());
        }
        /// <summary>
        ///  返回序列的唯一元素;如果该序列并非恰好包含一个元素,否则返回null。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static T SingleOrDefault <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.ToList().SingleOrDefault());
        }
 /// <summary>
 /// 将Queryable转换为分页后的List《T》集合
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="queryable"></param>
 /// <param name="pageIndex">当前页码</param>
 /// <param name="pageSize">每页显示数量</param>
 /// <returns></returns>
 public static List <T> ToPageList <T>(this Queryable <T> queryable, int pageIndex, int pageSize)
 {
     if (queryable.OrderBy.IsNullOrEmpty())
     {
         throw new Exception("分页必需使用.Order排序");
     }
     if (pageIndex == 0)
     {
         pageIndex = 1;
     }
     queryable.Skip = (pageIndex - 1) * pageSize;
     queryable.Take = pageSize;
     return(queryable.ToList());
 }
        /// <summary>
        ///   返回序列中的第一个元素,如果序列为NULL返回default(T)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <returns></returns>
        public static T FirstOrDefault <T>(this Queryable <T> queryable)
        {
            if (queryable.OrderBy.IsNullOrEmpty())
            {
                queryable.OrderBy = "GETDATE()";
            }
            queryable.Skip(0);
            queryable.Take(1);
            var reval = queryable.ToList();

            if (reval == null || reval.Count == 0)
            {
                return(default(T));
            }
            return(reval.First());
        }