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; }
}
}
- 下面是數據集的屏幕截圖:
我已保存把這個DataSet的數據轉換成csv文件,請找到它click here。
StuffEntity中的內部屬性與DataSet中的列標題具有相同的說明。
有沒有人給我一個方法來展示如何將這個數據集轉換爲StuffEntity?thx。
我不知道你是否可以使用實體框架,而不是手動進行。由於數據來自數據庫,因此使用EF更新模型將非常簡單。否則,反思方法必須解決。您的數據集是否像多個數據表或複雜關係一樣複雜? – natenho
使用反射,你的問題看起來像[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
@natenho DataSet中只有一個表。我看到了你提供的鏈接,但這不是一個通用的解決方案。有沒有可能寫一次轉換方法,但在其他地方使用?是否有可能反映財產描述轉換? – CharlieShi