简单的Datatable转List,Json

这里用到了Newtonsoft.Json,下载地址:http://json.codeplex.com/

1.根据不同的Model转为对应的List

 1 public static List<Model> ToList<Model>(this DataTable dt) where Model: class,new()  
 2 {  
 3   
 4     //创建一个属性的列表  
 5     List<PropertyInfo> prlist = new List<PropertyInfo>();  
 6     //获取Model的类型实例  反射的入口  
 7     Type t = typeof(Model);  
 8     //获得Model的所有的Public 属性 并找出Model属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表  
 9     Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); });  
10     //创建返回的集合  
11     List<Model> oblist = new List<Model>();  
12     foreach (DataRow row in dt.Rows)  
13     {  
14         //创建Model的实例  
15         var Modelob = new Model();  
16         //找到对应的数据,并赋值  
17         prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(Modelob, row[p.Name], null); });  
18         //放入到返回的集合中.  
19         oblist.Add(Modelob);  
20     }  
21     return oblist;  
22 }  

2.直接转为List<Hashtable>

把一些没有特定列的DataTable转为List<Hashtable>,一来方便使用到List<T>的功能,进行相关操作。二来,可以直接用MVC提供的Json()转为JsonResult。

 public List<Hashtable> GetList(DataTable dt)
        {
            List<Hashtable> mList = new List<Hashtable>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i <= count-1; i++)
                {
                    Hashtable ht = new Hashtable();
                    foreach (DataColumn col in dt.Columns)
                    {
                        ht.Add(col.ColumnName, dt.Rows[i][col.ColumnName]);
                    }
                    mList.Add(ht);
                }
            }
            return mList;
        }

 

 

3.DataTable转Json

//方法一:先转List,在转Json,可以附带上分页   
        public string DtToJson( DataTable dt , int page, int rows)
        {
            string json = string.Empty;
            IList<Hashtable> mList = new List<Hashtable>();
            int count = dt.Rows.Count;
            int pageCount = (page + rows) > count ? count : (page + rows);
            if (pageCount > 0)
            {
                for (int i = page; i <= pageCount - 1; i++)
                {
                    Hashtable ht = new Hashtable();
                    foreach (DataColumn col in dt.Columns)
                    {
                        ht.Add(col.ColumnName, dt.Rows[i][col.ColumnName]);
                    }
                    mList.Add(ht);
                }
                json = JsonConvert.SerializeObject(new {total=count,rows= mList });
            }
            return json;
        }
//方法二:非常简单,直接datatable转Json,但需要比较新版本的Newtonsoft.Json.dll 
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

public string DtToJson(DataTable dt)
{
result=JsonConvert.SerializeObject(dt, new DataTableConverter());
return result;
}
//方法三:转为List用mvc提供的Json转为Jsonresult
public ActionResult Select(int page, int rows)
{
    var UserList=GetList(SelectUserTable());
    var List = UserList.Skip((page - 1) * rows).Take(rows);
    int length = UserList.Count();
    var result = Json(new { total = length.ToString(), rows = List });
    result.ContentType = "text/html";
    return result;
}

 

 

 

转载于:https://www.cnblogs.com/woostundy/p/3305589.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值