如何在C#中将数据集转换为实体?
在这里,我提出了第一种方法:即在DataSet上使用foreach子句来获取项并填充实体。但是,当某些内容发生变化时,此方法无法重用 因此,我认为也许反思应该是我的场景中最好的方法。详情如下: 1.我的实体定义如下:如何在C#中将数据集转换为实体?,c#,dataset,entity,C#,Dataset,Entity,在这里,我提出了第一种方法:即在DataSet上使用foreach子句来获取项并填充实体。但是,当某些内容发生变化时,此方法无法重用 因此,我认为也许反思应该是我的场景中最好的方法。详情如下: 1.我的实体定义如下: using System.ComponentModel; namespace WarningServiceForYM.Model { public class StuffEntity { [Description("企业ID")] public str
using System.ComponentModel;
namespace WarningServiceForYM.Model
{
public class StuffEntity
{
[Description("企业ID")]
public string EnterpriseID { get; set; }
[Description("冰柜编号")]
public string FridgeID { get; set; }
[Description("冰柜名称")]
public string FridgeName { get; set; }
[Description("手机号码")]
public string PhoneNumber { get; set; }
[Description("设备名称")]
public string EquipmentName { get; set; }
[Description("采集参数")]
public string PickingParams { get; set; }
[Description("一路温度")]
public string TempOne { get; set; }
[Description("二路温度")]
public string TempTwo { get; set; }
[Description("三路温度")]
public string TempThree { get; set; }
[Description("四路温度")]
public string TempFour { get; set; }
[Description("五路温度")]
public string TempFive { get; set; }
[Description("六路温度")]
public string TempSix { get; set; }
[Description("七路温度")]
public string TempSeven { get; set; }
[Description("八路温度")]
public string TempEight { get; set; }
[Description("温度最低")]
public string Min { get; set; }
[Description("温度最高")]
public string Max { get; set; }
[Description("采集时间")]
public string PickingTime { get; set; }
[Description("通知间隔")]
public string WarningPeriod { get; set; }
[Description("延时")]
public string PendingTime { get; set; }
[Description("通知开关")]
public string Switch { get; set; }
[Description("最后通知时间")]
public string LastInformTime { get; set; }
}
}
public static T GetEntity<T>(DataRow row) where T : new()
{
var entity = new T();
var properties = typeof(T).GetProperties();
foreach (var property in properties)
{
//Get the description attribute
var descriptionAttribute = (DescriptionAttribute)property.GetCustomAttributes(typeof(DescriptionAttribute), true).SingleOrDefault();
if (descriptionAttribute == null)
continue;
property.SetValue(entity, row[descriptionAttribute.Description]);
}
return entity;
}
publicstatict GetEntity(DataRow行),其中T:new()
{
var entity=newt();
var properties=typeof(T).GetProperties();
foreach(属性中的var属性)
{
//获取description属性
var descriptionAttribute=(descriptionAttribute)属性.GetCustomAttributes(typeof(descriptionAttribute),true).SingleOrDefault();
if(descriptionAttribute==null)
继续;
SetValue(实体,行[descriptionAttribute.Description]);
}
返回实体;
}
您可以像这样使用它:
foreach (DataRow dataRow in dataSet.Tables[0].Rows)
{
var e = GetEntity<StuffEntity>(dataRow);
Console.WriteLine(e.EnterpriseID);
}
foreach(dataSet.Tables[0].Rows中的DataRow数据行)
{
var e=GetEntity(数据行);
控制台写入线(e.EnterpriseID);
}
它是一个通用实现,因此您可以将它用于任何其他类型或您想要的数据集。
我尽可能地简化了它,因此可以通过添加一些一致性来广泛改进它,比如在设置实体值之前检查列名是否存在,或者根据需要验证重复的描述。例如,它还可以转换为DataRow、DataTable或DataSet的扩展方法。我想知道您是否可以使用Entity Framework而不是手动操作。由于数据来自数据库,因此使用EF更新模型将非常简单。否则,反射方法必须解决这个问题。您的数据集是否像拥有多个数据表或关系一样复杂?使用反射,您的问题看起来像数据集中一个表的副本,并且@natenho只有一个表。我看到了你给出的链接,但它不是一个通用的解决方案。有没有可能只写一次convert方法而在其他地方使用?是否有可能反映要转换的属性描述?@natenho这是我项目中负责消息发送的一个小部件。EF对我来说太重了。谢谢。