Click here to Skip to main content
15,511,503 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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>
Posted
Updated 5-Aug-11 2:08am
v2

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

or alternatively
C#
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:
C#
IEnumerable<Person> allAgedTwenty = myList.Where<Person>( x => return x.Age == 20; );
IEnumerable<int> indices = allAgedTwenty.Select<Person,int>( x => myList.IndexOf(x) );


The first case will get you only one int and the second case will leave you with a list of ints.

Best Regards,

—MRB
 
Share this answer
 
v3
Comments
fjdiewornncalwe 5-Aug-11 8:16am    
You get my five for encouraging the OP to use LINQ for this purpose.
Manfred Rudolf Bihy 5-Aug-11 8:34am    
Thanks Marcus,
I've only recently started using LinQ since we stepped from .NET 2.0 to 4.0 just a short while ago. I must say that I love it. It's so awesome in expressing combinations, transformations and filtering. Code that used to take up 10 to 15 lines can easily be expressed in 2 - 3 lines. And once you've gotten used to the syntax it's not so cryptic after all. :)
BobJanova 5-Aug-11 10:38am    
5 for good and appropriate use of LINQ.
[no name] 5-Aug-11 11:31am    
Nice and clear, my 5!
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;
      }
   }        
}


Index will be 2, since items are stored like 0,1,2,3 etc in the List.
 
Share this answer
 
v5
Comments
Manfred Rudolf Bihy 5-Aug-11 7:31am    
My 4!
Close, but not quite there. What happens in your code if there is more than one person aged twenty?
UJimbo 5-Aug-11 7:48am    
I'm using this case only, like the OP requested :)
Manfred Rudolf Bihy 5-Aug-11 8:37am    
Still doesn't do what OP wanted. You set n to zero which would be a valid index. So when no Person aged twenty is found your code would suggest the first entry on the list would fit.
At least set n to an invalid number for an index, like -1 for instance.
Manfred Rudolf Bihy 5-Aug-11 12:17pm    
Looks better now! Have my 5!
vishal_h 5-Aug-11 8:10am    
I try it but it gives me nullReferenceException and warn me to used new keyword..
if i used new like Person item=new Person()...then it gives error in foreach(item in MyList) line....what should i do sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900