如何在C#中将DataSet转换为实体?

5

我将提供第一种方法:使用DataSet的foreach子句来获取项目并填充您的实体。但是,当某些内容发生更改时,此方法无法重复使用。

因此,我认为反射可能是我场景下最好的方法。以下是详细信息:

1.我的实体定义如下:

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; }  
    }
}

以下是DataSet的截图: enter image description here 我已将此DataSet的数据保存为csv文件,请点击此处查找。
在StuffEntity中的内部属性具有与DataSet中列标题相同的描述。
是否有人能够提供一种方法,展示如何将这个DataSet转换为StuffEntity?谢谢。

2
我想知道你是否可以使用Entity Framework代替手动操作。由于数据来自数据库,使用EF更新模型非常简单明了。否则,必须采用反射方法解决。你的数据集是否复杂,例如包含多个数据表或关联? - natenho
1
使用反射,你的问题看起来像是这个问题的重复:这里这里 - natenho
@natenho DataSet中只有一个表。我看到了你提供的链接,但那不是通用解决方案。有没有可能编写一次转换方法,但在其他地方使用?有没有可能反射属性描述来进行转换? - CharlieShi
@natenho 这是我项目中负责消息发送的小部件。EF 对我来说太重了。谢谢。 - CharlieShi
2个回答

8

好的,使用反射:

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;
}

您可以像这样使用它:
foreach (DataRow dataRow in dataSet.Tables[0].Rows)
{
    var e = GetEntity<StuffEntity>(dataRow);
    Console.WriteLine(e.EnterpriseID);
}

这是一个通用的实现,所以您可以将其与任何其他类型或数据集一起使用。我尽可能地简化了它,因此可以通过添加某些一致性来广泛改进,例如在设置实体值之前检查列名是否存在或根据需要验证重复描述。它还可以转换为DataRow、DataTable或DataSet的扩展方法。


0

在我的情况下,你的代码几乎可以工作,但是descriptionAttribute始终为空,因此它会继续执行直到我没有返回结果。

var descriptionAttribute = (DescriptionAttribute)property.GetCustomAttributes(typeof(DescriptionAttribute), true).SingleOrDefault();
                if (descriptionAttribute == null)
                    continue;

我不确定变量descriptionAttribute发生了什么事情,它一直是null。

所以我将@natenho的代码更改为这样,对我有用。

 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;

                if (row[property.Name].ToString() != "")
                {
                    property.SetValue(entity, row[property.Name]);
                }

            }

            return entity;
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接