【问题标题】:Adding rows to collections not working向集合添加行不起作用
【发布时间】:2013-07-29 11:17:12
【问题描述】:

我正在尝试向现有集合添加新行/值。但添加行后显示相同的旧结果。

//代码

Using CustomActionWorkflow As New CustomActionWorkflow()
        CustomActionWorkflow.WorkflowId = Me.WorkflowId
        CustomActionWorkflow.CustomActionId = Me.CustomActionId
        Me.CustomActionsController.CustomActionsWorkflowCollection.ToList().Add(CustomActionWorkflow)
End Using

我哪里错了?

【问题讨论】:

  • CustomActionsWorkflowCollection proeprty 的类型是什么?
  • @nemesv:它的属性集合。

标签: .net vb.net collections


【解决方案1】:

ToList() 函数为您提供 IEnumerable 集合的新副本。您尝试添加到您没有参考的列表中。

试试 CustomActionsWorkflowCollection 是否有 Add 方法

Me.CustomActionsController.CustomActionsWorkflowCollection.Add(CustomActionWorkflow)

或者是否可以设置集合

    var list = Me.CustomActionsController.CustomActionsWorkflowCollection.ToList(); 
list.Add(CustomActionWorkflow);
Me.CustomActionsController.CustomActionsWorkflowCollection = list;

但最后一个不是一个好的解决方案,因为创建了很多临时列表。

【讨论】:

  • 您要我添加而不转换为列表吗?
  • 给你一个完整的答案我们需要知道Me.CustomActionsController.CustomActionsWorkflowCollection的类型
  • Me.CustomActionsController.CustomActionsWorkflowCollection = Me.CustomActionsController.CustomActionsWorkflowCollection.ToList().Add(CustomActionWorkflow) 不起作用,因为 List<T>.Add 的返回类型为 void
猜你喜欢
  • 2022-01-25
  • 2012-08-03
  • 1970-01-01
  • 2019-12-02
  • 1970-01-01
  • 1970-01-01
  • 2018-06-24
  • 2020-04-15
  • 1970-01-01
相关资源
最近更新 更多