簡體   English   中英

在 C# web 表單中,如何使用泛型類型從數據集中加載列表?

[英]In C# web forms, how can I load a List from a dataset using generic types?

我正在嘗試將數據集中的內容映射到列表中,但我希望能夠將類型傳遞給函數。 我已經有以下代碼:

public class WorkOrderAttachment : DatabaseObject
{
    [TableData]
    //public List<int> attachment_id { get; set; }
    public int attachment_id { get; set; }
    [TableData]
    //public List<int> wo_id { get; set; }
    public int wo_id { get; set; }

    public WorkOrderAttachment()
    {

    }

. . .

public class DatabaseObject
{
    private string _ConnectionString = "";

. . .

    public static List<T> LoadDataAll<T>(T type, DataSet ds)
    {
        List<T> fillList = new List<T>();
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                foreach (var prop in type.GetType().GetProperties())
                {
                    if (prop.GetCustomAttributes(typeof(TableData), false).Length > 0 && ds.Tables[0].Columns.Contains(prop.Name.ToLower()))
                    {
                        if (dr[prop.Name.ToLower()] != DBNull.Value)
                        {
                            prop.SetValue(type, Convert.ChangeType(dr[prop.Name.ToLower()], prop.PropertyType), null);
                        }
                        else
                        {
                            // If we are using a string and the value is null, default to an empty string instead
                            if (prop.PropertyType == typeof(String))
                            {
                                if (dr[prop.Name.ToLower()] == DBNull.Value)
                                {
                                    prop.SetValue(type, "", null);
                                }
                            }
                        }
                    }
                }
                fillList.Add(type);
            }
        }
        return fillList;
    }

. . .

我是這樣使用它的:

return DatabaseObject.LoadDataAll<WorkOrderAttachment>(new WorkOrderAttachment(), ds);

這有效,但假設數據集中的表中有兩行。 第一次循環時,它會到達 fullList.Add(type) 並將數據放置到位。 “attachment_id = 1,wo_id = 1000”

但是,下一次循環時,它會設置“attachment_id = 2, wo_id = 1000”。 一切看起來都不錯,但事實並非如此。 當我檢查fillList時,我發現了兩個條目,但它們都是重復的最后一項“attachment_id = 2, wo_id = 1000”。 我猜第一個實例(我知道它已經到位)因為它是通過引用處理的方式而被覆蓋的。

(我不想在這種情況下使用實體框架或其他 ORM,但是將數據從數據集中獲取到 List 的其他選項將不勝感激。)

我希望能夠指定這樣的東西(偽代碼):

List<WorkOrderAttachment> Attachments = DatabaseResult();

但是,我也希望它是通用的,這樣我就可以將任何類型放入 DatabaseResult 並將我的響應作為已加載數據的列表。 任何有關如何解決此問題或實施更好的解決方案的建議表示贊賞。

我在 StackOverflow 的另一個問題中找到了答案,盡管我的問題與這個問題不同。 答案是一樣的: 在 C# 中,如何在方法內部實例化傳遞的泛型類型?

public static List<T> LoadDataAll<T>(DataSet ds) where T : new()
{

. . .

    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        T type = new T();
        foreach (var prop in type.GetType().GetProperties())
        {

. . .

使用 where T : new() 語法,我可以為每一行創建一個新類型,並將其傳遞到發回的 fillList 中。 我對這個方法的使用如下:

return DatabaseObject.LoadDataAll<WorkOrderAttachment>(ds);

返回的結果是一個 WorkOrderAttachment 列表,我只是傳入數據集。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM