2013-01-14 63 views
-2

我有一個列表,並希望在列表填充後添加兩個新屬性。將一個新的屬性添加到列表

可以這樣做嗎?

C#列表

List<Usp_GetListItemsBySystemResult> PendingListOfItemsToControl = new List<Usp_GetListItemsBySystemResult>(); // Items to Control in Running memory 

// Get items to Control into running memory 
FAItemsUnderControl fItemsUnderControl = new FAItemsUnderControl(ConfigurationManager.AppSettings["DatabaseConnectionString"]); 
PendingListOfItemsToControl = fItemsUnderControl.getItemsUnderControl(DropDownListSystems.Text.ToString(), "ALL").ToList<Usp_GetListItemsBySystemResult>(); 

// Add new paramaters to the list here ???????? 

C#新的屬性添加

public string ItemRequestStatus { get; set; } // Determines the change status if applicable 
    public bool IsButtonEnabled { get; set; } // Determines if the delete button is enabled or disabled 

C#類

public class Usp_GetListItemsBySystemResult 
{ 
    public Usp_GetListItemsBySystemResult(); 

    public DateTime? Creation_DateTime { get; set; } 
    public string Item_Backup_Location { get; set; } 
    public string Item_Category { get; set; } 
    public short? Item_Check_Interval_Time { get; set; } 
    public DateTime? Item_Creation_DateTime { get; set; } 
    public DateTime? Item_Last_Access_DateTime { get; set; } 
    public DateTime? Item_Last_Modified_DateTime { get; set; } 
    public string Item_Name { get; set; } 
    public string Item_Path { get; set; } 
    public int? Item_Size { get; set; } 
    public string Item_Status { get; set; } 
    public string Item_Value { get; set; } 
    public string Item_Value_SHA256 { get; set; } 
    public int? ItemUnderControl_ID { get; set; } 
} 
+3

這可能是一個語言的事情,但你的問題沒有任何意義。 'List's沒有「參數」,所以我不明白你想要完成什麼。 – CodingGorilla

+0

現在更新了這個問題。謝謝 – user1438082

+1

作爲班級的一部分,你是否需要屬性作爲列表的一部分? – Oded

回答

1

你應該用List繼承這2個屬性創建屬於自己的MyList類。 事情是這樣的:

public class MyList<T> : List<T> 
{ 
    public string ItemRequestStatus { get; set; } // Determines the change status if applicable 
    public bool IsButtonEnabled { get; set; } // Determines if the delete button is enabled or disabled 

    public MyList() 
    { 

    } 

    public MyList(IList<T> list) 
     : base(list) 
    { 

    } 
} 

用法:

FAItemsUnderControl fItemsUnderControl = new FAItemsUnderControl(ConfigurationManager.AppSettings["DatabaseConnectionString"]); 
var list = fItemsUnderControl.getItemsUnderControl(DropDownListSystems.Text.ToString(), "ALL").ToList<Usp_GetListItemsBySystemResult>(); 
MyList<Usp_GetListItemsBySystemResult> PendingListOfItemsToControl = new MyList<Usp_GetListItemsBySystemResult>(list); 
+1

取決於他希望在List上還是存儲在List中的對象上。問題不清楚,但這是不常見的情況。 – Bobson

+0

你能舉個例子嗎? – user1438082

+0

我希望它在名單上。最終我打算把'新'列表的gridview – user1438082

1

您可以創建這個類,並使用LINQ to值添加到這個新的屬性。

List<NewClass> = from x in GetListItemsBySystemResult 
       select new NewClass{ 
        Creation_DateTime = x.Creation_DateTime , 
        ..., 
        ItemRequestStatus = "Value", 
        IsButtonEnabled = "Value", 
        }; 

然後在這個新的列表你已經填充了新的價值。

1

您可以創建一個的expando對象和任何你想要添加到它:

var myx = new ExpandoObject() as IDictionary<string, Object>; 
myx.Add("NewProp", string.Empty);