DataSet和List<T> 泛型之间互相转换 (转载, 作者写的很好)

http://www.cnblogs.com/envelope/archive/2010/04/20/1716343.html
001//DataSet与泛型集合间的互相转换
002//利用反射机制将DataTable的字段与自定义类型的公开属性互相赋值。
003//注意:从DataSet到IList<T>的转换,自定义类型的公开属性必须与DataTable中的字段名称
004//一致,才能到达想要的结果。建议DataTable的定义从数据库来,自定义类型用O/R Mapping的方式获得。
005 
006//代码说明
007 
008 
009 
010/// <summary>
011/// 泛型集合与DataSet互相转换
012/// </summary>
013using System.Data;
014using System.Reflection;
015using System.Collections;
016using System.Collections.Generic;
017using System;
018public class IListDataSet
019{
020 
021    /// <summary>
022    /// 集合装换DataSet
023    /// </summary>
024    /// <param name="list">集合</param>
025    /// <returns></returns>
026    /// 2008-08-01 22:08 HPDV2806
027    public static DataSet ToDataSet(IList p_List)
028    {
029        DataSet result = new DataSet();
030        DataTable _DataTable = new DataTable();
031        if (p_List.Count > 0)
032        {
033            PropertyInfo[] propertys = p_List[0].GetType().GetProperties();
034            foreach (PropertyInfo pi in propertys)
035            {
036                _DataTable.Columns.Add(pi.Name, pi.PropertyType);
037            }
038 
039            for (int i = 0; i < p_List.Count; i++)
040            {
041                ArrayList tempList = new ArrayList();
042                foreach (PropertyInfo pi in propertys)
043                {
044                    object obj = pi.GetValue(p_List[i], null);
045                    tempList.Add(obj);
046                }
047                object[] array = tempList.ToArray();
048                _DataTable.LoadDataRow(array, true);
049            }
050        }
051        result.Tables.Add(_DataTable);
052        return result;
053    }
054 
055    /// <summary>
056    /// 泛型集合转换DataSet
057    /// </summary>
058    /// <typeparam name="T"></typeparam>
059    /// <param name="list">泛型集合</param>
060    /// <returns></returns>
061    /// 2008-08-01 22:43 HPDV2806
062    public static DataSet ToDataSet<T>(IList<T> list)
063    {
064        return ToDataSet<T>(list, null);
065    }
066 
067 
068    /// <summary>
069    /// 泛型集合转换DataSet
070    /// </summary>
071    /// <typeparam name="T"></typeparam>
072    /// <param name="p_List">泛型集合</param>
073    /// <param name="p_PropertyName">待转换属性名数组</param>
074    /// <returns></returns>
075    /// 2008-08-01 22:44 HPDV2806
076    public static DataSet ToDataSet<T>(IList<T> p_List, params string[] p_PropertyName)
077    {
078        List<string> propertyNameList = new List<string>();
079        if (p_PropertyName != null)
080            propertyNameList.AddRange(p_PropertyName);
081 
082        DataSet result = new DataSet();
083        DataTable _DataTable = new DataTable();
084        if (p_List.Count > 0)
085        {
086            PropertyInfo[] propertys = p_List[0].GetType().GetProperties();
087            foreach (PropertyInfo pi in propertys)
088            {
089                if (propertyNameList.Count == 0)
090                {
091                    // 没有指定属性的情况下全部属性都要转换
092                    _DataTable.Columns.Add(pi.Name, pi.PropertyType);
093                }
094                else
095                {
096                    if (propertyNameList.Contains(pi.Name))
097                        _DataTable.Columns.Add(pi.Name, pi.PropertyType);
098                }
099            }
100 
101            for (int i = 0; i < p_List.Count; i++)
102            {
103                ArrayList tempList = new ArrayList();
104                foreach (PropertyInfo pi in propertys)
105                {
106                    if (propertyNameList.Count == 0)
107                    {
108                        object obj = pi.GetValue(p_List[i], null);
109                        tempList.Add(obj);
110                    }
111                    else
112                    {
113                        if (propertyNameList.Contains(pi.Name))
114                        {
115                            object obj = pi.GetValue(p_List[i], null);
116                            tempList.Add(obj);
117                        }
118                    }
119                }
120                object[] array = tempList.ToArray();
121                _DataTable.LoadDataRow(array, true);
122            }
123        }
124        result.Tables.Add(_DataTable);
125        return result;
126    }
127 
128    /// <summary>
129    /// DataSet装换为泛型集合
130    /// </summary>
131    /// <typeparam name="T"></typeparam>
132    /// <param name="p_DataSet">DataSet</param>
133    /// <param name="p_TableIndex">待转换数据表索引</param>
134    /// <returns></returns>
135    /// 2008-08-01 22:46 HPDV2806
136    public static IList<T> DataSetToIList<T>(DataSet p_DataSet, int p_TableIndex)
137    {
138        if (p_DataSet == null || p_DataSet.Tables.Count < 0)
139            return null;
140        if (p_TableIndex > p_DataSet.Tables.Count - 1)
141            return null;
142        if (p_TableIndex < 0)
143            p_TableIndex = 0;
144 
145        DataTable p_Data = p_DataSet.Tables[p_TableIndex];
146        // 返回值初始化
147        IList<T> result = new List<T>();
148        for (int j = 0; j < p_Data.Rows.Count; j++)
149        {
150            T _t = (T)Activator.CreateInstance(typeof(T));
151            PropertyInfo[] propertys = _t.GetType().GetProperties();
152            foreach (PropertyInfo pi in propertys)
153            {
154                for (int i = 0; i < p_Data.Columns.Count; i++)
155                {
156                    // 属性与字段名称一致的进行赋值
157                    if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
158                    {
159                        // 数据库NULL值单独处理
160                        if (p_Data.Rows[j][i] != DBNull.Value)
161                            pi.SetValue(_t, p_Data.Rows[j][i], null);
162                        else
163                            pi.SetValue(_t, null, null);
164                        break;
165                    }
166                }
167            }
168            result.Add(_t);
169        }
170        return result;
171    }
172 
173    /// <summary>
174    /// DataSet装换为泛型集合
175    /// </summary>
176    /// <typeparam name="T"></typeparam>
177    /// <param name="p_DataSet">DataSet</param>
178    /// <param name="p_TableName">待转换数据表名称</param>
179    /// <returns></returns>
180    /// 2008-08-01 22:47 HPDV2806
181    public static IList<T> DataSetToIList<T>(DataSet p_DataSet, string p_TableName)
182    {
183        int _TableIndex = 0;
184        if (p_DataSet == null || p_DataSet.Tables.Count < 0)
185            return null;
186        if (string.IsNullOrEmpty(p_TableName))
187            return null;
188        for (int i = 0; i < p_DataSet.Tables.Count; i++)
189        {
190            // 获取Table名称在Tables集合中的索引值
191            if (p_DataSet.Tables[i].TableName.Equals(p_TableName))
192            {
193                _TableIndex = i;
194                break;
195            }
196        }
197        return DataSetToIList<T>(p_DataSet, _TableIndex);
198    }
199}
200 
201 
202/*****************
203使用范围
204 
2051. 可以用在业务层中数据获取,获取DataSet的同时也可以转为IList集合为调用者所使用。
206 
2072. 在WebServices中传输自定义类型使用,即传递参数都用DataSet类型(WebServices直接支持的数据类型),在使用前将其转换为IList来使用。
208 * ******************************/

转载于:https://www.cnblogs.com/infim/archive/2011/03/31/2001380.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值