获取列表中的项目

JL.*_*JL. 8 c# list

我有以下列表项

public List<Configuration> Configurations
{
    get;
    set;
}

 public class Configuration
  {
    public string Name
     {
       get;
       set;
     }
    public string Value
      {
       get;
       set;
     }
 }
Run Code Online (Sandbox Code Playgroud)

如何在配置中提取name = value的项目?

例如:假设我在该列表中有100个配置对象.

我怎样才能得到:Configurations.name ["myConfig"]

那样的东西?

更新:请.net v2的解决方案

Gre*_*ech 18

使用List<T>.FindC#3.0中的方法:

var config = Configurations.Find(item => item.Name == "myConfig");
Run Code Online (Sandbox Code Playgroud)

在C#2.0/.NET 2.0中,您可以使用类似下面的内容(语法可能稍微偏离,因为我在相当长的时间内没有以这种方式编写代理...):

Configuration config = Configurations.Find(
    delegate(Configuration item) { return item.Name == "myConfig"; });
Run Code Online (Sandbox Code Playgroud)


Amb*_*ber 5

看起来你真正想要的是一个词典(http://msdn.microsoft.com/en-us/library/xfhwa508.aspx).

字典专门用于映射键值对,并为查找提供比List更好的性能.