在asp.net中如何将DataSet类型的数据集转换为List类型

DataSet ds = SelectForList(where); private DataSet SelectForList(string where) { string sql = "select a.*,p.PlaceName,d.prdtname from VD_StockDuctBarCode a " + "inner join tabplace p on a.placeid = p.Placeid " + "inner join tabPrdtType d on a.TypeID = d.TypeID "; if (where != "") sql += " where " + where; return SqlHelper.Select(sql); } private List<StocktableSubLst> DataLst // 子表列表 { get { return (List<StocktableSubLst>)Session["BillSub"]; } set { Session["BillSub"] = value; } } 如何将ds的值负给DataLst

第1个回答  2011-08-24
给你的StocktableSubLst 类写好构造函数,字段与你数据库查出的字段对应
例如你StocktableSubLst 类有两个字段string A 和 string B
构造函数为
public StocktableSubLst(string A, string B)
{
this.A=A;
this.B=B;
}
然后遍历赋值增加
List<StocktableSubLst> lt=null;
if(ds.tables.count>0)
{
lt=new List<StocktableSubLst>(ds.Tables[0].Rows.Count);
foreach(DataRow row in ds.Tables[0].Rows)
{
lt.Add(new StocktableSubLst(row["A"].ToString(),row["B"].ToString()));
}
}
第2个回答  2011-08-24
DataTable dt = ds.Tables[0];
List<StocktableSubLst> myList= DataTableToList<StocktableSubLst>(dt);

public static List<T> DataTableToList<T>(DataTable dt)
where T : new()
{
List<T> mylist = new List<T>();
if (dt != null && dt.Rows.Count != 0)
{
PropertyInfo[] pInfos = typeof(T).GetProperties();
foreach (DataRow dr in dt.Rows)
{
T obj = new T();
mylist.Add(obj);
foreach (PropertyInfo info in pInfos)
{
try
{
info.SetValue(obj, dr[info.Name], null);
}
catch
{
}
}
}
}
return mylist;
}
第3个回答  2011-08-24
需要遍历DataSet
List<T> lst=new List<T>();
if(ds.tables.count>0)
{
foreach(datarow row in ds.tables[0].rows)
{
lst.Add.......
}
}本回答被网友采纳
相似回答