C#将DataTable转换成list的方法

2019-12-26 12:11:30刘景俊
易采站长站为您分析C#将DataTable转换成list的方法,实例分析了DataTable转换成list及数据分页的技巧,需要的朋友可以参考下    

本文实例讲述了C#将DataTable转换成list及数据分页的方法。。具体如下:

 

复制代码 /// <summary>  
 /// 酒店评论列表-分页  
/// </summary>  
/// <param name="userId"></param>  
/// <param name="pageIndex">当前页</param>  
/// <param name="pageCount">总页数</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   
telorder.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"),