Пример #1
0
        /// <summary>
        ///   返回序列中的第一个元素,如果序列为NULL返回default(T)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <returns></returns>
        public static T FirstOrDefault <T>(this  Queryable <T> queryable)
        {
            var reval = queryable.ToList();

            if (reval == null || reval.Count == 0)
            {
                return(default(T));
            }
            return(reval.First());
        }
 /// <summary>
 ///  返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。
 /// </summary>
 /// <typeparam name="T">表实体类型</typeparam>
 /// <param name="queryable">查询对象</param>
 /// <returns>T</returns>
 public static T Single <T>(this  Queryable <T> queryable)
 {
     if (queryable.OrderByValue.IsNullOrEmpty())
     {
         queryable.OrderByValue = "now()";
     }
     queryable.Skip(0);
     queryable.Take(1);
     return(queryable.ToList().Single());
 }
Пример #3
0
        /// <summary>
        ///  返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static T Single <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().Single());
        }
        /// <summary>
        ///  返回序列中的第一个元素。
        /// </summary>
        /// <typeparam name="T">表实体类型</typeparam>
        /// <param name="queryable">查询对象</param>
        /// <returns>T</returns>
        public static T First <T>(this  Queryable <T> queryable)
        {
            if (queryable.OrderByValue.IsNullOrEmpty())
            {
                queryable.OrderByValue = "now()";
            }
            queryable.Skip(0);
            queryable.Take(1);
            var reval = queryable.ToList();

            return(reval.First());
        }
Пример #5
0
 /// <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>T</returns>
        public static T FirstOrDefault <T>(this  Queryable <T> queryable)
        {
            if (queryable.OrderByValue.IsNullOrEmpty())
            {
                queryable.OrderByValue = "now()";
            }
            queryable.Skip(0);
            queryable.Take(1);
            var reval = queryable.ToList();

            if (reval == null || reval.Count == 0)
            {
                return(default(T));
            }
            return(reval.First());
        }
Пример #7
0
        /// <summary>
        ///  返回序列中的第一个元素。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <returns></returns>
        public static T First <T>(this  Queryable <T> queryable)
        {
            var reval = queryable.ToList();

            return(reval.First());
        }
Пример #8
0
 /// <summary>
 ///  返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="queryable"></param>
 /// <returns></returns>
 public static T Single <T>(this  Queryable <T> queryable)
 {
     return(queryable.ToList().Single());
 }