C# 如何将数据集中的值添加到列表中?
我想添加我得到的数据集,它是一个ID列表,格式如下:C# 如何将数据集中的值添加到列表中?,c#,asp.net,linq,ado.net,C#,Asp.net,Linq,Ado.net,我想添加我得到的数据集,它是一个ID列表,格式如下: string str = "Select bd_id from [Active]"; ds = new DataSet(str); da = new SqlDataAdapter(str, con); da.Fill(ds); 作为项目导入到通用列表中 如何做同样的事情?使用LINQ to DATATABLE可以轻松完成任务 bd_id 1 2 3 DataTable dtDetails=ds.Table[0]; 列出当
string str = "Select bd_id from [Active]";
ds = new DataSet(str);
da = new SqlDataAdapter(str, con);
da.Fill(ds);
作为项目导入到通用列表中
如何做同样的事情?使用LINQ to DATATABLE可以轻松完成任务
bd_id
1
2
3
DataTable dtDetails=ds.Table[0];
列出当前代码=
(来自dtDetails.AsEnumerable()中的dr)
选择dr.Field(“bd_id”).ToList();
SqlDataAdapter sda=新的SqlDataAdapter(sql,conn);
数据集ds=新数据集();
sda.填写(ds,“表格”);
IList lists=GetList(ds.Tables[“table”]);
//DataTable转换为列表方法
公共列表GetList(数据表)
{
列表=新列表();
T=默认值(T);
PropertyInfo[]PropertyTypes=null;
string tempName=string.Empty;
foreach(table.Rows中的DataRow行)
{
t=Activator.CreateInstance();
PropertyTypes=t.GetType().GetProperties();
foreach(属性类型中的PropertyInfo pro)
{
tempName=pro.Name;
if(table.Columns.Contains(tempName))
{
对象值=行[tempName];
if(value.GetType()==typeof(System.DBNull))
{
值=空;
}
pro.SetValue(t,value,null);
}
}
列表。添加(t);
}
退货清单;
}
这对我有什么帮助?如果你能找到一些,会有帮助的steps@vini-您现在可以查看示例代码…您和google linq都可以访问datable并获取有关此的详细信息…我希望您了解C的linq#
DataTable dtDetails = ds.Table[0];
List<int> lstExcelCurrencyCode =
(from dr in dtDetails.AsEnumerable()
select dr.Field<int>("bd_id")).ToList<int>();
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "table");
IList<T> lists = GetList<T>(ds.Tables["table"]);
//DataTable Convert To List Method
public List<T> GetList<T>(DataTable table)
{
List<T> list = new List<T>();
T t = default(T);
PropertyInfo[] propertypes = null;
string tempName = string.Empty;
foreach (DataRow row in table.Rows)
{
t = Activator.CreateInstance<T>();
propertypes = t.GetType().GetProperties();
foreach (PropertyInfo pro in propertypes)
{
tempName = pro.Name;
if (table.Columns.Contains(tempName))
{
object value = row[tempName];
if (value.GetType() == typeof(System.DBNull))
{
value = null;
}
pro.SetValue(t, value, null);
}
}
list.Add(t);
}
return list;
}