问题描述
大家好,我需要知道如何将datatable的列添加到列表中并显示该列的值,实际上我已经完成了示例应用程序,如果类结构具有属性,但是我已经正常运行了,我已经2我不想创建两个单独的表,我需要一个可以正常工作的实体,我将提供用于属性的示例代码,请为我提供列表
病人类别
{
公共int剂量{放; }
公共字符串Drug {get;放; }
公共字符串Patient1 {get;放; }
公开的DateTime日期{放; }
}
课程计划
{
静态void Main(string [] args)
{
病人p =新的Patient();
DataTable表= GetTable();
List<患者> lst = GetTable().ToCollection<患者>();
foreach(第一位患者cn)
{
Console.WriteLine(cn.Drug);
}
Console.Read();
}
静态DataTable GetTable()
{
DataTable表=新的DataTable();
table.Columns.Add(剂量",typeof(int));
table.Columns.Add("Drug",typeof(string));
table.Columns.Add("Patient",typeof(string));
table.Columns.Add("Date",typeof(DateTime));
table.Rows.Add(25,"Indocin","David",DateTime.Now);
table.Rows.Add(50,"Enebrel","Sam",DateTime.Now);
table.Rows.Add(10,"Hydralazine","Christoff",DateTime.Now);
table.Rows.Add(21,"Combivent","Janet",DateTime.Now);
table.Rows.Add(100,"Dilantin","Melanie",DateTime.Now);
返回表;
}
}
Hi Everyone i need to know how i can add the the columns of datatable to the list and display that column values of that actually i have done sample application which is working properly for if the classs structure have the properties but i ve 2 tables i don''t want to create the two separate entities i need one entity which can be work properly and i will give my sample code which is for properties please help me for the list
class Patient
{
public int Dosage { get; set; }
public string Drug { get; set; }
public string Patient1 { get; set; }
public DateTime Date { get; set; }
}
class Program
{
static void Main(string[] args)
{
Patient p = new Patient();
DataTable table = GetTable();
List<Patient> lst = GetTable().ToCollection<Patient>();
foreach (Patient cn in lst)
{
Console.WriteLine(cn.Drug);
}
Console.Read();
}
static DataTable GetTable()
{
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
return table;
}
}
public static class MyExtensionClass
{
public static List<T> ToCollection<T>(this DataTable dt)
{
List<T> lst = new System.Collections.Generic.List<T>();
Type tClass = typeof(T);
PropertyInfo[] pClass = tClass.GetProperties();
List<DataColumn> dc = dt.Columns.Cast<DataColumn>().ToList();
T cn;
foreach (DataRow item in dt.Rows)
{
cn = (T)Activator.CreateInstance(tClass);
foreach (PropertyInfo pc in pClass)
{
DataColumn d = dc.Find(c => c.ColumnName == pc.Name);
if (d != null)
pc.SetValue(cn, item[pc.Name], null);
}
lst.Add(cn);
}
return lst;
}
}
现在我的问题是我想这样做
病人类别
{
公共int剂量{放; }
pulic iList< string> lst = new system.generic.List< string>();
}
而不是3个属性,我必须使用该列表,并且必须进行以上工作,任何人都可以对此进行帮助.....
now my problem is i want to do like this
class Patient
{
public int Dosage { get; set; }
pulic iList<string>lst=new system.generic.List<string>();
}
instead of that 3 properties i have to use the list and the above work has to be happen can anyone help with this please........
推荐答案
这篇关于将datatabel动态转换为通用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!