我有一个列表,想要在填充列表后添加两个新属性。
可以这样做吗?
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#class
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; }
}
答案 0 :(得分:1)
您应该使用从MyList
继承的这2个属性创建自己的List
类。
像这样:
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 :(得分:1)
您可以创建此类并使用Linq为此新属性添加值。
List<NewClass> = from x in GetListItemsBySystemResult
select new NewClass{
Creation_DateTime = x.Creation_DateTime ,
...,
ItemRequestStatus = "Value",
IsButtonEnabled = "Value",
};
然后在这个新列表中,您填充了新值。
答案 2 :(得分:1)
您可以创建一个expando对象并添加您想要的任何内容:
var myx = new ExpandoObject() as IDictionary<string, Object>;
myx.Add("NewProp", string.Empty);