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;
}
怎麼會,幫助我?將有助於如果你可以站點幾個步驟 – vini 2012-02-04 06:29:18
@vini - 你現在可以看到示例代碼...你也可以谷歌LINQ到datable並獲得有關此信息的詳細信息..我希望你知道C#的LINQ# – 2012-02-04 06:32:32
它完成謝謝: ) – vini 2012-02-04 06:36:31