Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 5-Aug-11 1:37am
vishal_h1.8K
Edited 5-Aug-11 2:08am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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) );
 
The first case will get you only one int and the second case will leave you with a list of ints.
 
Best Regards,
 
—MRB
  Permalink  
v3
Comments
Marcus Kramer at 5-Aug-11 8:16am
   
You get my five for encouraging the OP to use LINQ for this purpose.
Manfred R. Bihy at 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 at 5-Aug-11 10:38am
   
5 for good and appropriate use of LINQ.
Shameel at 5-Aug-11 11:31am
   
Nice and clear, my 5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v5
Comments
Manfred R. Bihy at 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 at 5-Aug-11 7:48am
   
I'm using this case only, like the OP requested :)
Manfred R. Bihy at 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 R. Bihy at 5-Aug-11 12:17pm
   
Looks better now! Have my 5!
vishal_h at 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
UJimbo at 5-Aug-11 8:37am
   
Using your Person class, try using the above function, n will be 2 at the end
UJimbo at 5-Aug-11 8:40am
   
person should be Person while defining the ObservableCollection, for some reason the page code marks it as an html tag and then tries closing it at the end. Probaly a bug in the web page
BobJanova at 5-Aug-11 10:38am
   
I edited your solution to make the generic type parameters not fight with HTML tags.
UJimbo at 5-Aug-11 11:43am
   
Thanks bob, I've got to rtfm at some point

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



Advertise | Privacy | Mobile
Web03 | 2.8.150224.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100