DataTable转List<dynamic>

DataTable转List<dynamic>

最近做的一个项目,MVC+Ado.net。没有ORM很不习惯。找到一个办法,DataTable转List<dynamic>,这样代码就比较好看一点,主要是为了配合 
Razor好用点。本来想自己写一个,结果发现网上已经有人写好了。直接拿来用吧。
来源:http://www.oschina.net/code/snippet_1011399_54272

有过滤字段,和反转过滤字段。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public  class  DYController : Controller
{
     
     public  ActionResult Index()
     {
         DataTable dt =  new  DataTable();
         dt.Columns.Add( "Id" );
         dt.Columns.Add( "Name" );
 
         dt.Rows.Add(1,  "刘备" );
         dt.Rows.Add(2,  "关羽" );
         dt.Rows.Add(3,  "张飞" );
 
         List<dynamic> ListPerson = dt.ToDynamicList();
         ViewBag.ListPerson = ListPerson;
         return  View();
     }
}
 
/// <summary>
/// DataTable 扩展
/// </summary>
public  static  class  DataTableExtensions
{
     /// <summary>
     /// 将DataTable 转换成 List<dynamic>
     /// reverse 反转:控制返回结果中是只存在 FilterField 指定的字段,还是排除.
     /// [flase 返回FilterField 指定的字段]|[true 返回结果剔除 FilterField 指定的字段]
     /// FilterField  字段过滤,FilterField 为空 忽略 reverse 参数;返回DataTable中的全部数
     /// </summary>
     /// <param name="table">DataTable</param>
     /// <param name="reverse">
     /// 反转:控制返回结果中是只存在 FilterField 指定的字段,还是排除.
     /// [flase 返回FilterField 指定的字段]|[true 返回结果剔除 FilterField 指定的字段]
     ///</param>
     /// <param name="FilterField">字段过滤,FilterField 为空 忽略 reverse 参数;返回DataTable中的全部数据</param>
     /// <returns>List<dynamic></returns>
     public  static  List<dynamic> ToDynamicList( this  DataTable table,  bool  reverse =  true params  string [] FilterField)
     {
         var  modelList =  new  List<dynamic>();
         foreach  (DataRow row  in  table.Rows)
         {
             dynamic model =  new  ExpandoObject();
             var  dict = (IDictionary< string object >)model;
             foreach  (DataColumn column  in  table.Columns)
             {
                 if  (FilterField.Length != 0)
                 {
                     if  (reverse ==  true )
                     {
                         if  (!FilterField.Contains(column.ColumnName))
                         {
                             dict[column.ColumnName] = row[column];
                         }
                     }
                     else
                     {
                         if  (FilterField.Contains(column.ColumnName))
                         {
                             dict[column.ColumnName] = row[column];
                         }
                     }
                 }
                 else
                 {
                     dict[column.ColumnName] = row[column];
                 }
             }
             modelList.Add(model);
         }
         return  modelList;
     }
}

  页面代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
< table >
     < tr >
         < th >Id</ th >
         < th >Name</ th >
     </ tr >
     @foreach (dynamic item in ViewBag.ListPerson)
     {
         < tr >
             < th >@item.Id</ th >
             < th >@item.Name</ th >
         </ tr >
     }
</ table >

  

DataTable转List<dynamic>仅仅是由于习惯了ORM的写法,实际上点出来也没有代码提示,意义不大。

dynamic做数据传递,实际上比较方便的是用于后端代码接收前端提交过来的JSON,然后转dynamic解析比较直观易懂可直接点出属性,而且不用写太多与之一一对应的Model类。

但是后端向前端传JSON的话,还是用数据字典Dictionary比较好。因为到写前端代码的时候也依然可以点出来:Dic[id] = 1、Dic[Name] = "关羽" => data.Id、 data.Name。


来源:

http://www.cnblogs.com/kissdodog/p/5683979.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将一个DaTaTable换为List<T>,可以使用以下步骤: 1. 创建一个类来表示DaTaTable中的每一行数据。这个类应该包含与DaTaTable中的列对应的属性。 2. 创建一个List<T>对象,其中T是第一步中创建的类。 3. 使用foreach循环遍历DaTaTable中的每一行数据。 4. 在循环中,创建T类型的对象,并将DaTaTable中的每一列的值赋值给T对象的相应属性。 5. 将T对象添加到List<T>中。 6. 返回List<T>对象。 以下是一个示例代码: ``` public class MyDataClass { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } } public List<MyDataClass> ConvertDataTableToList(DataTable dt) { List<MyDataClass> list = new List<MyDataClass>(); foreach (DataRow row in dt.Rows) { MyDataClass obj = new MyDataClass(); obj.Id = Convert.ToInt32(row["Id"]); obj.Name = row["Name"].ToString(); obj.Description = row["Description"].ToString(); list.Add(obj); } return list; } ``` 在这个示例中,我们创建了一个名为MyDataClass的类来表示DaTaTable中的每一行数据。我们还创建了一个名为ConvertDataTableToList的方法,该方法接受一个DataTable对象作为参数,并将其换为List<MyDataClass>对象。在方法中,我们使用foreach循环遍历DaTaTable中的每一行数据,并为每一行数据创建一个MyDataClass对象。然后,我们将DaTaTable中的每一列的值赋值给MyDataClass对象的相应属性,并将MyDataClass对象添加到List<MyDataClass>中。最后,我们返回List<MyDataClass>对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值