c# - 如何使用和 C# 更新集合中的项目

标签 c# entity-framework generics

前段时间我问了一个问题here “如何使用 Linq 和 C# 在集合中添加项目”

例如我有相同的收藏:

      List<Subscription> subscription = new List<Subscription>
       {
           new Subscription{Type = "Trial", Period = 30 },
           new Subscription{Type = "Free", Period = 90 },
           new Subscription{Type = "Paid", Period = 365 }
       };

现在我想使用 C# 代码更新此列表中的一个项目。可能吗?

最佳答案

您的订阅列表包含对 3 个订阅对象的 3 个引用。如果您更新该列表中的 3 个订阅对象中的任何一个(通过任何方式),则该列表将引用更新的元素。

subscription.First(s => s.Period == 30).Trial = "new";

将更新订阅对象。列表不会更改,但它引用已更新的同一对象

关于c# - 如何使用和 C# 更新集合中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7649243/

相关文章:

c# - 如何使用 AutoMapper 更新具有嵌套实体的实体并使用 Entity Framework 保存更新后的实体?

scala - @uncheckedVariance 在 Kotlin 中?

java - 具有扩展泛型类型的方法签名 "incorrect"

c# - 将一个泛型参数约束为另一个的子类型

c# - RazorEngine 布局

c# - 如何从 C# 获取 COM 组件的 ProgID?

c# - ASP.NET MVC4 一 View 多模型

c# - 为什么是 "it is recommended to include foreign key property in the entity class"?

c# - ConfigureAwait(false) 没有按预期使 HttpContext NULL

c# - 端口 pascal const IdentChars = ['a' 。 .'z' , 'A' 。 .'Z' , '_' ];对 C# 的声明