C# 通过 id 获取列表项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15299640/
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-10 16:21:41  来源:igfitidea点击:

get list item by id

c#visual-studio-2010list

提问by tux007

I have a list:

我有一个清单:

List<string> theList = new List<string>;

There are a few elements in the List. And now I want to get an item by the index. e.g. I want to get element number 4. How can I do it?

List 中有几个元素。现在我想通过索引获取一个项目。例如,我想获得第 4 号元素。我该怎么做?

回答by Zbigniew

To get 4th item, you can use indexer:

要获得第 4 项,您可以使用索引器:

string item = theList[3];

if you prefer to use methods, then you can use ElementAtor (ElementAtOrDefault):

如果您更喜欢使用方法,那么您可以使用ElementAt或 ( ElementAtOrDefault):

string item = theList.ElementAt(3);

回答by JaredPar

Just use the indexer

只需使用索引器

string item = theList[3];

Note that indexes in C# are 0 based. So if you want the 4th element in the list you need to use index 3. If you want the 5th element you would use index 4. It's unclear from your question which you intended

请注意,C# 中的索引是基于 0 的。因此,如果您想要列表中的第 4 个元素,您需要使用索引 3。如果您想要第 5 个元素,您将使用索引 4。从您的问题中不清楚您的意图

The indexer is a common feature on .Net collection types. For lists it's generally index based, for maps it's key based. The documentation for the type in question will tell you which and if it's available. The indexer member will be listed though as the property named Item

索引器是 .Net 集合类型的常见功能。对于列表,它通常基于索引,对于地图,它基于键。相关类型的文档会告诉您它是否可用以及它是否可用。索引器成员将作为名为的属性列出Item

http://msdn.microsoft.com/en-us/library/0ebtbkkc.aspx

http://msdn.microsoft.com/en-us/library/0ebtbkkc.aspx

回答by Parimal Raj

You can use the Indexerto get the Item at selected index

您可以使用Indexer来获取选定的项目index

string item = theList[3];

回答by Federico Berasategui

use the Indexer Syntax:

使用索引器语法

var fourthItem = theList[3];

回答by Tom Riley

this should do it, access via array index.

这应该这样做,通过数组索引访问。

theList[3] 

its 3 as index's start at 0.

它的 3 作为索引从 0 开始。

回答by Tim Schmelter

Use the indexer:

使用索引器:

string the4th = theList[3];

Note that this throws an exception if the list has only 3 items or less since the index is always zero-based. You might want to use Enumerable.ElementAtOrDefaultthen:

请注意,如果列表只有 3 个或更少的项目,这将引发异常,因为索引始终从零开始。那么你可能想使用Enumerable.ElementAtOrDefault

string the4th = theList.ElementAtOrDefault(3);
if(the4th != null)
{
    // ...
}

ElementAtOrDefaultreturns the element at the specified index if index < list.Countand default(T)if index >= theList.Count. So for reference types(like String) it returns nulland for value types their default value(e.g. 0 for int).

ElementAtOrDefault返回指定索引处的元素 ifindex < list.Countdefault(T)if index >= theList.Count。因此,对于引用类型(如String),它返回,null而对于值类型,它们的默认值(例如 0 for int)。

For collection types which implement IList<T>(arrays or lists) it uses the indexer to get the element, for other types it uses a foreachloop and a counter variable.

对于实现IList<T>(数组或列表)的集合类型,它使用索引器来获取元素,对于其他类型,它使用foreach循环和计数器变量。

So you could also use the Countproperty to check if the list contains enough items for the index:

因此,您还可以使用该Count属性来检查列表是否包含足够的索引项:

string the4th = null;
if(index < theList.Count)
{
    the4th = theList[index];
}

回答by prograshid

You can use the Indexer to get the Item at selected index

您可以使用索引器获取所选索引处的项目

     string item = theList[3];

Or if u want to get the id (if accessing from database) define a class e.g.

或者,如果您想获取 id(如果从数据库访问),请定义一个类,例如

    public class Person
    {
      public int PId;
      public string PName;

    }

and access as follow

并按如下方式访问

     List<Person> theList = new List<Person>();
    Person p1 = new Person();
    int id = theList[3].PId