将DataTable转换成list 及数据分页。
<1>
/// <summary> /// 酒店评论列表-分页 /// </summary> /// <param name="userId"></param> /// <param name="pageIndex">当前页</param> /// <returns></returns> public static List<CommentInfo> GetHotelCommentList(int userId, int pageIndex, out int pageCount) { var list = new List<CommentInfo>(); pageCount = 0; try { //查询酒店ID,名字,图片,用户ID,用户评论 string sql = string.Format( @"select hotels.hid,hotels.hotelName,hotels.images,hotelorder.UserID,user_HotelComment.comment from hotels with(nolock) join hotelorder with(nolock) join user_HotelComment on hotelorder.UserID=user_HotelComment.userID on hotels.hid=hotelorder.HotelID where hotelorder.UserID={0}", userId); DataTable dt = SQLHelper.Get_DataTable(sql, SQLHelper.GetCon(), null); if (dt != null && dt.Rows.Count > 0) { list = (from p in dt.AsEnumerable() //这个list是查出全部的用户评论 select new CommentInfo { Id = p.Field<int>("hid"), //p.Filed<int>("Id") 其实就是获取DataRow中ID列。即:row["ID"] HotelImages = p.Field<string>("images"), HotelName = p.Field<string>("hotelName"), Comment = p.Field<string>("comment") }).ToList(); //将这个集合转换成list int pageSize = 10; pageCount = list.Count % pageSize == 0 ? list.Count / pageSize : (list.Count / pageSize) + 1;//获取总页数 //这个list 就是取到10条数据 list = list.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList(); } } catch (Exception ex) { // write log here } return list; }
时间: 2024-12-18 04:06:37