【问题标题】:add a new property to a list [closed]将新属性添加到列表中[关闭]
【发布时间】:2013-01-14 19:33:26
【问题描述】:

我有一个列表,想在列表填充后添加两个新属性。

这个可以吗?

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; }
}

【问题讨论】:

  • 这可能是语言问题,但您的问题没有任何意义。 Lists 没有“参数”,所以我无法理解您要完成的工作。
  • 您是否需要将这些属性作为列表的一部分或作为类的一部分?
  • 为什么不将它们添加到类中并根据需要进行设置?
  • @user1438082 - 举个例子说明你想如何设置ItemRequestStatus,这对我们来说可能更有意义。当前下面的所有三个答案都是您问题的完全可能答案,即使它们都做了非常不同的事情。
  • 我不明白为什么您列表中的对象被称为Usp_GetListItemsBySystemResult,在阅读答案时,我认为这个名称造成了一些混乱。

标签: c# asp.net list


【解决方案1】:

您应该使用继承自List 的这两个属性创建自己的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);

【讨论】:

  • 取决于他是想在List 上还是在List 中存储的对象上。问题尚不清楚,但这是不太常见的情况。
  • 你能给我举个例子吗?
  • 我希望它在列表中。最终我会将“新”列表绑定到网格视图
  • @user1438082 写的,不知道,这些属性是不是应该依赖于你列表中的数据?
  • 您好,您能否详细说明如何从我的基类继承并继续将 PendingListOfItemsToControl 结果放入这个新对象中?
【解决方案2】:

您可以创建这个类并使用 Linq 向这个新属性添加值。

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

然后在这个新列表中,您已经填充了新值。

【讨论】:

    【解决方案3】:

    你可以创建一个 expando 对象并添加任何你想要的东西:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      相关资源
      最近更新 更多