2013-10-10 20 views
3

在這裏我想出了第一種方法:就是使用DataSet上的foreach子句來獲取項目並填寫您的實體。但是當這些東西發生變化時,這種方法不能被重用。如何將DataSet轉換爲C#中的實體?

所以我認爲也許反射應該是我的方案的最佳方法。下面是詳細信息:

1.My實體定義如下:

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; } 
    } 
} 
  • 下面是數據集的屏幕截圖: enter image description here
  • 我已保存把這個DataSet的數據轉換成csv文件,請找到它click here

    StuffEntity中的內部屬性與DataSet中的列標題具有相同的說明。

    有沒有人給我一個方法來展示如何將這個數據集轉換爲StuffEntity?thx。

    +2

    我不知道你是否可以使用實體框架,而不是手動進行。由於數據來自數據庫,因此使用EF更新模型將非常簡單。否則,反思方法必須解決。您的數據集是否像多個數據表或複雜關係一樣複雜? – natenho

    +1

    使用反射,你的問題看起來像[this]的副本(http://stackoverflow.com/questions/17107220/convert-dataset-to-object-list)和[this](http://stackoverflow.com/ questions/8008389/how-to-convert-datatable-to-class-object) – natenho

    +0

    @natenho DataSet中只有一個表。我看到了你提供的鏈接,但這不是一個通用的解決方案。有沒有可能寫一次轉換方法,但在其他地方使用?是否有可能反映財產描述轉換? – CharlieShi

    回答

    5

    好,使用反射:

    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的擴展方法。

    相關問題