C# 将数据表转换为列表<;实体>;(德古拉计划)
我需要一个静态方法将DataTables(动态)转换为List(同样是动态实体) 这是我的密码 我会很感激你的帮助C# 将数据表转换为列表<;实体>;(德古拉计划),c#,entity-framework,model-view-controller,C#,Entity Framework,Model View Controller,我需要一个静态方法将DataTables(动态)转换为List(同样是动态实体) 这是我的密码 我会很感激你的帮助 public static ICollection<System.Data.Entity.Core.Objects.DataClasses.EntityObject> DtToEntity(DataTable DataTable,System.Data.Entity.Core.Objects.DataClasses.EntityObject Entity
public static ICollection<System.Data.Entity.Core.Objects.DataClasses.EntityObject> DtToEntity(DataTable DataTable,System.Data.Entity.Core.Objects.DataClasses.EntityObject EntityObject)
{
ICollection<System.Data.Entity.Core.Objects.DataClasses.EntityObject> _list = null;
System.Data.Entity.Core.Objects.DataClasses.EntityObject _tempClass;
foreach (DataRow dataRow in DataTable.Rows)
{
foreach(DataColumn dataColumn in DataTable.Columns)
{
foreach (var attribute in EntityObject.GetType().GetProperties())
{
if (attribute.Name == dataColumn.ColumnName && attribute.GetType().Equals(dataColumn.GetType()))
{
return _list;
}
}
}
}
公共静态ICollection DtToEntity(DataTable DataTable、System.Data.Entity.Core.Objects.DataClasses.EntityObject EntityObject)
{
ICollection _list=null;
System.Data.Entity.Core.Objects.DataClasses.EntityObject_tempClass;
foreach(DataTable.Rows中的DataRow-DataRow)
{
foreach(DataTable.Columns中的DataColumn DataColumn)
{
foreach(EntityObject.GetType().GetProperties()中的var属性)
{
如果(attribute.Name==dataColumn.ColumnName&&attribute.GetType().Equals(dataColumn.GetType()))
{
返回列表;
}
}
}
}
私有静态列表转换数据表(数据表dt)
{
列表数据=新列表();
foreach(数据行中的数据行)
{
T项=获取项(行);
数据。添加(项目);
}
返回数据;
}
私有静态T GetItem(DataRow dr)
{
温度类型=温度类型(T);
T obj=Activator.CreateInstance();
foreach(dr.Table.Columns中的DataColumn列)
{
foreach(temp.GetProperties()中的PropertyInfo pro)
{
if(pro.Name==column.ColumnName)
pro.SetValue(obj,dr[column.ColumnName],null);
其他的
继续;
}
}
返回obj;
}
用法:
List< Student > studentDetails = new List<Student>();
studentDetails = ConvertDataTable<Student>(dt);
ListstudentDetails=newlist();
studentDetails=ConvertDataTable(dt);
来源:因此,基本上,您希望在mvc.net核心应用程序中使用此结构,并使用依赖项注入和ado.net进行orm。首先出现的问题是,在使用ado.net时,您必须手动将数据表映射到c对象。但经过多年的发展,它得到了发展,现在您可以轻松地在您的应用程序上实现此结构应用程序是简洁的功能。我承认这是一个小规模的应用程序。但我认为这是从sql server获得结果的最快方式。就个人而言,你可能会使用简洁的应用程序,我也建议你这样做。这是一个很好的代码和平,我认为值得去维基以供将来参考 您可以从数据库上下文工厂注入的存储库开始。我将工厂模式用于数据库上下文,因为您可能希望在应用程序上使用sql server的多个实例。然后需要工厂模式。我建议为您的基本信息创建一个小的数据库,并将该工厂设置为单例。这样可以节省大量时间e和获取数据的工作将首先被消除。在repo的第一种方法中,有示例功能来展示您的设计,因此您使用factory获取结果,并通过上一个答案中提供的covert data table函数将其转换为对象(非常感谢@Nikhil Vartak)。原来是这样的!。在后面的文章中,我介绍了将数据表函数转换到这篇文章中,这是这个问题的主要原因。其他部分是正常的.net core或.net normality的一部分,这篇文章不关心
/* repo */
public class repo
{
private readonly IDBContextFactory dBContextFactory;
public repo(IDBContextFactory _dbContextFactory)
{
_dbContextFactory=dBContextFactory;
}
public string GetLastRecord()
{
List< Student > studentDetails = new List<Student>();
studentDetails = ConvertDataTable<Student>(_dbCtextFactory.Select("mydb","select * from StudentDetail");
/*refrence from this post https://stackoverflow.com/questions/33515552/converting-datatable-to-listentity-projectdracula */;
}
}
/* interface of repo */
public interface IRepo
{
public string GetLastRecord();
}
/* controller */
public class mycontroller:BaseController
{
private readonly IRepo repo;
public mycontroller(IRepo _repo)
{
_repo=repo;
}
[httpGet]
public IActionResult<string> GetLastRecord()
{
return _repo.GetLastRecord();
}
}
/* some factory pattern for db context (multiple dbs) */
public class DBContextFactory
{
private SqlCommand BuildFactory(string dbName)
{
switch(dbName)
{
case 'mydb':
return CreateMyDB();
}
}
private SqlCommand CreateMyDB()
{
string connectionString = "your connection string";
SqlConnection connection =
new SqlConnection(connectionString));
SqlCommand command = new SqlCommand(connection);
return command.Open();
}
//Private SqlCommand GetMyOpenCommand()
public DataTable Select(string dbName,string query)
{
SqlDataAdapter dataAdapter=new SqlDataAdapter();
dataAdapter.SelectCommand=BuildFactory(dbName);
DataTable dt =new DataTable();
dataAdapter.Fill(dt);
con.Close();
return dt;
}
}
/* factory in dependncy pattern */
public inteface IDBContextFactory
{
SqlCommand BuildFactory(string dbName);
SqlCommand CreateMyDB();
DataTable Select(string dbName,string query)
}
/****** HERE IS YOUR GENERIC FILE ******/
private static List<T> ConvertDataTable<T>(DataTable dt)
{
List<T> data = new List<T>();
foreach (DataRow row in dt.Rows)
{
T item = GetItem<T>(row);
data.Add(item);
}
return data;
}
private static T GetItem<T>(DataRow dr)
{
Type temp = typeof(T);
T obj = Activator.CreateInstance<T>();
foreach (DataColumn column in dr.Table.Columns)
{
foreach (PropertyInfo pro in temp.GetProperties())
{
if (pro.Name == column.ColumnName)
pro.SetValue(obj, dr[column.ColumnName], null);
else
continue;
}
}
return obj;
}
/***** END OF GENERIC PART *****/
/* USAGE OF GENERIC */
List< Student > studentDetails = new List<Student>();
studentDetails = ConvertDataTable<Student>(dt);
/*repo*/
公开回购
{
私有只读IDBContextFactory dBContextFactory;
公开回购(IDBContextFactory\U dbContextFactory)
{
_dbContextFactory=dbContextFactory;
}
公共字符串GetLastRecord()
{
ListstudentDetails=newlist();
studentDetails=ConvertDataTable(_dbCtextFactory.Select(“mydb”,“Select*fromStudentDetail”);
/*引用此帖子https://stackoverflow.com/questions/33515552/converting-datatable-to-listentity-projectdracula */;
}
}
/*回购的接口*/
公共接口IRepo
{
公共字符串GetLastRecord();
}
/*控制器*/
公共类mycontroller:BaseController
{
私人只读IRepo回购协议;
公共mycontroller(IRepo\u repo)
{
_回购=回购;
}
[httpGet]
公共IActionResult GetLastRecord()
{
返回_repo.GetLastRecord();
}
}
/*数据库上下文的某些工厂模式(多个数据库)*/
公共类DBContextFactory
{
私有SqlCommand BuildFactory(字符串dbName)
{
开关(dbName)
{
案例“mydb”:
返回CreateMyDB();
}
}
私有SqlCommand CreateMyDB()
{
string connectionString=“您的连接字符串”;
SqlConnection连接=
新的SqlConnection(connectionString));
SqlCommand=newsqlcommand(连接);
return命令.Open();
}
//私有SqlCommand GetMyOpenCommand()
公共数据表选择(字符串dbName、字符串查询)
{
SqlDataAdapter dataAdapter=新的SqlDataAdapter();
/* repo */
public class repo
{
private readonly IDBContextFactory dBContextFactory;
public repo(IDBContextFactory _dbContextFactory)
{
_dbContextFactory=dBContextFactory;
}
public string GetLastRecord()
{
List< Student > studentDetails = new List<Student>();
studentDetails = ConvertDataTable<Student>(_dbCtextFactory.Select("mydb","select * from StudentDetail");
/*refrence from this post https://stackoverflow.com/questions/33515552/converting-datatable-to-listentity-projectdracula */;
}
}
/* interface of repo */
public interface IRepo
{
public string GetLastRecord();
}
/* controller */
public class mycontroller:BaseController
{
private readonly IRepo repo;
public mycontroller(IRepo _repo)
{
_repo=repo;
}
[httpGet]
public IActionResult<string> GetLastRecord()
{
return _repo.GetLastRecord();
}
}
/* some factory pattern for db context (multiple dbs) */
public class DBContextFactory
{
private SqlCommand BuildFactory(string dbName)
{
switch(dbName)
{
case 'mydb':
return CreateMyDB();
}
}
private SqlCommand CreateMyDB()
{
string connectionString = "your connection string";
SqlConnection connection =
new SqlConnection(connectionString));
SqlCommand command = new SqlCommand(connection);
return command.Open();
}
//Private SqlCommand GetMyOpenCommand()
public DataTable Select(string dbName,string query)
{
SqlDataAdapter dataAdapter=new SqlDataAdapter();
dataAdapter.SelectCommand=BuildFactory(dbName);
DataTable dt =new DataTable();
dataAdapter.Fill(dt);
con.Close();
return dt;
}
}
/* factory in dependncy pattern */
public inteface IDBContextFactory
{
SqlCommand BuildFactory(string dbName);
SqlCommand CreateMyDB();
DataTable Select(string dbName,string query)
}
/****** HERE IS YOUR GENERIC FILE ******/
private static List<T> ConvertDataTable<T>(DataTable dt)
{
List<T> data = new List<T>();
foreach (DataRow row in dt.Rows)
{
T item = GetItem<T>(row);
data.Add(item);
}
return data;
}
private static T GetItem<T>(DataRow dr)
{
Type temp = typeof(T);
T obj = Activator.CreateInstance<T>();
foreach (DataColumn column in dr.Table.Columns)
{
foreach (PropertyInfo pro in temp.GetProperties())
{
if (pro.Name == column.ColumnName)
pro.SetValue(obj, dr[column.ColumnName], null);
else
continue;
}
}
return obj;
}
/***** END OF GENERIC PART *****/
/* USAGE OF GENERIC */
List< Student > studentDetails = new List<Student>();
studentDetails = ConvertDataTable<Student>(dt);