简单的Datatable转List,Json_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 简单的Datatable转List,Json

简单的Datatable转List,Json

 2013/9/7 21:12:33  woostundy  博客园  我要评论(0)
  • 摘要:这里用到了Newtonsoft.Json,下载地址:http://json.codeplex.com/1.根据不同的Model转为对应的List1publicstaticList<Model>ToList<Model>(thisDataTabledt)whereModel:class,new()2{34//创建一个属性的列表5List<PropertyInfo>prlist=newList<PropertyInfo>()
  • 标签:list JSON JS

这里用到了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         Modelob = new Model();  
16         //找到对应的数据,并赋值  
17         prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); });  
18         //放入到返回的集合中.  
19         oblist.Add(ob);  
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;
}

 

 

 

发表评论
用户名: 匿名