如何在C#中获取列表项的索引 [英] how to get index of list item in C#

查看:590
本文介绍了如何在C#中获取列表项的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到年龄为20的列表项的索引..我可以使用list的indexOf()方法...现在在这种情况下它的索引是3 ...但是如何找到它和显示...

I want to find the index of list item whose age is 20 ..how can i get using the indexOf() method of list...now in this case its index is 3...but how to find it and display...

public class Person
   {
       public string Name { get; set; }
       public int Age { get; set; }
       public string Country { get; set; }
       public string EmailId { get; set; }
       public DateTime JoinedOn { get; set; }
   }




ObservableCollection<person> myList = new ObservableCollection<Person>()
           {
               new Person{ Name="Person 1", Age=21, Country="India",  EmailId="some@some.com", JoinedOn=DateTime.Now},
               new Person{ Name="Person 2", Age=29, Country="India",  EmailId="some@some.com", JoinedOn=DateTime.Now},
               new Person{ Name="Person 3", Age=20, Country="India",  EmailId="some@some.com", JoinedOn=DateTime.Now},
               new Person{ Name="Person 4", Age=22, Country="India",  EmailId="some@some.com", JoinedOn=DateTime.Now},
               new Person{ Name="Person 5", Age=23, Country="India",  EmailId="some@some.com", JoinedOn=DateTime.Now},

           };
</person>

推荐答案

如果您只想要一个结果,请执行以下操作:

In case you always want only one result do this:
Person agedTwenty = myList.Where<Person>( x => return x.Age == 20; ).Single<Person>();
int index = myList.IndexOf(agedTwenty);



或者


or alternatively

int index = myList.Where<Person>( x => return x.Age == 20; ).Select<Person,int>( x => myList.IndexOf(x)).Single<int>();





如果可以有多个结果,你可以这样做:



In case there can be more than one result you'd do this:

IEnumerable<Person> allAgedTwenty = myList.Where<Person>( x => return x.Age == 20; );
IEnumerable<int> indices = allAgedTwenty.Select<Person,int>( x => myList.IndexOf(x) );





第一种情况只会给你一个int而第二种情况会给你一个整数列表。



最好的问候,



-MRB


在列表项目中进行foreach循环:



Make a foreach loop through your List items:

private void LookForPerson() 
{
   ObservableCollection<Person> myList = new ObservableCollection<Person>()
   {
      new Person{ Name="Person 1", Age=21, Country="India",  EmailId="some@some.com", JoinedOn=DateTime.Now},
      new Person{ Name="Person 2", Age=29, Country="India",  EmailId="some@some.com", JoinedOn=DateTime.Now},
      new Person{ Name="Person 3", Age=20, Country="India",  EmailId="some@some.com", JoinedOn=DateTime.Now},
      new Person{ Name="Person 4", Age=22, Country="India",  EmailId="some@some.com", JoinedOn=DateTime.Now},
      new Person{ Name="Person 5", Age=23, Country="India",  EmailId="some@some.com", JoinedOn=DateTime.Now},
 
   };

   int n = -1;
   foreach (Person item in myList)
   {
      if (item.Age == 20)
      {
          n = myList.IndexOf(item);
          break;
      }
   }        
}





索引将为2,因为项目存储为0,列表中的1,2,3等。



Index will be 2, since items are stored like 0,1,2,3 etc in the List.


这篇关于如何在C#中获取列表项的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆