【发布时间】:2009-04-14 23:42:04
【问题描述】:
如何在以下特定场景中使用 Linq 查找和替换属性:
public interface IPropertyBag { }
public class PropertyBag : IPropertyBag
{
public Property[] Properties { get; set; }
public Property this[string name]
{
get { return Properties.Where((e) => e.Name == name).Single(); }
//TODO: Just copying values... Find out how to find the index and replace the value
set { Properties.Where((e) => e.Name == name).Single().Value = value.Value; }
}
}
感谢您提前提供帮助。
【问题讨论】:
-
您使用的是什么 PropertyBag?我认为.Net BCL 中没有。我问是因为“属性”吸气剂可能是进行克隆的人,因此你不能做你想做的事情。
-
Jonathan,这是我自己的自定义 PropertyBag 类。上面的代码有效,因为我没有替换 Property[] 数组中的整个项目。代码中的注释说明: //TODO: Just copying values... 了解如何查找索引并替换值
-
只是想知道您为什么不使用我的无 LINQ IndexOf() 解决方案……现在我知道了!我将答案从实例更正为静态方法。
标签: c# linq linq-to-objects .net-3.0