获取列表中的项目 [英] Getting an item in a list
本文介绍了获取列表中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下列表项
public List<Configuration> Configurations
{
get;
set;
}
public class Configuration
{
public string Name
{
get;
set;
}
public string Value
{
get;
set;
}
}
如何在名称=值的配置中提取项目?
How can I pull an item in configuration where name = value?
例如:假设我在该列表中有100个配置对象.
For example: lets say I have 100 configuration objects in that list.
如何获取:Configurations.name ["myConfig"]
How can I get : Configurations.name["myConfig"]
类似的东西吗?
更新:.net v2解决方案请
推荐答案
使用 C#3.0中的List<T>.Find
方法:
var config = Configurations.Find(item => item.Name == "myConfig");
在C#2.0/.NET 2.0中,您可以使用类似以下的内容(语法可能会略有偏离,因为很长一段时间以来我都没有以这种方式编写委托...):
In C# 2.0 / .NET 2.0 you can use something like the following (syntax could be slightly off as I haven't written delegates in this way in quite a long time...):
Configuration config = Configurations.Find(
delegate(Configuration item) { return item.Name == "myConfig"; });
这篇关于获取列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文