C# 获取列表中的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1216438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:46:15  来源:igfitidea点击:

Getting an item in a list

c#list

提问by JL.

I have the following list item

我有以下列表项

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?

如何在名称 = 值的配置中提取项目?

For example: lets say I have 100 configuration objects in that list.

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

How can I get : Configurations.name["myConfig"]

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

Something like that?

类似的东西?

UPDATE: Solution for .net v2 please

更新:请解决.net v2

采纳答案by Greg Beech

Using the List<T>.Findmethod in C# 3.0:

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

var config = Configurations.Find(item => item.Name == "myConfig");

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...):

在 C# 2.0 / .NET 2.0 中,您可以使用类似以下内容(语法可能稍有偏差,因为我已经很长时间没有以这种方式编写委托了...):

Configuration config = Configurations.Find(
    delegate(Configuration item) { return item.Name == "myConfig"; });

回答by weiqure

Try List(T).Find(C# 3.0):

尝试列表(T)。查找(C# 3.0):

string value = Configurations.Find(config => config.Name == "myConfig").Value;

回答by Dykam

Consider using a Dictionary, but if not:

考虑使用字典,但如果不是:



You question wasn't fully clear to me, one of both should be your answer.

你的问题对我来说并不完全清楚,两者之一应该是你的答案。

using Linq:

使用 Linq:

var selected = Configurations.Where(conf => conf.Name == "Value");

or

或者

var selected = Configurations.Where(conf => conf.Name == conf.Value);

If you want it in a list:

如果你想把它放在一个列表中:

List<Configuration> selected = Configurations
    .Where(conf => conf.Name == "Value").ToList();

or

或者

List<Configuration> selected = Configurations
    .Where(conf => conf.Name == conf.Value).ToList();

回答by Amber

It seems like what you really want is a Dictionary (http://msdn.microsoft.com/en-us/library/xfhwa508.aspx).

看起来您真正想要的是字典(http://msdn.microsoft.com/en-us/library/xfhwa508.aspx)。

Dictionaries are specifically designed to map key-value pairs and will give you much better performance for lookups than a List would.

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

回答by Jason Evans

Here's one way you could use:

这是您可以使用的一种方法:

static void Main(string[] args)
        {
            Configuration c = new Configuration();
            Configuration d = new Configuration();
            Configuration e = new Configuration();

            d.Name = "Test";
            e.Name = "Test 23";

            c.Configurations = new List<Configuration>();

            c.Configurations.Add(d);
            c.Configurations.Add(e);

            Configuration t = c.Configurations.Find(g => g.Name == "Test");
        }