Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: list
Hi,
I need to find a index of Object/item stored in List.
It contains about 250 items.
 
I used List.findindex method, but i am not able to use Predicate delegate.
I have read many articles but i am not able to understand it.
 
Please help me finding the item in List.
 
Here is piece of code.
System.Collections.Generic.List<string> AllRanks = new List<string>();
 
It contains eg:
1. ABC
2. BBC
 
Thanks in advance.
Posted 16-May-11 0:00am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Better code would be using anonymous method:
 
System.Collections.Generic.List<string> list =
    new System.Collections.Generic.List<string>();
 
//...

int index = list.FindIndex(new System.Predicate<string>(
    (value) => {
        return value == "ABC" || value == "BBC";
}));
 
—SA
  Permalink  
Comments
prathameshpitale at 16-May-11 6:14am
   
Thanx!
SAKryukov at 16-May-11 6:23am
   
You're very welcome.
Thanks for accepting this answer.
Good luck, call again.
--SA
Albin Abel at 16-May-11 6:34am
   
My 5
SAKryukov at 16-May-11 6:43am
   
Thank you, Albin.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

private static bool Findlogic(string item)
{
 if(item == "ABC" || item == "BBC")
    return true;
 else
    return false
}
 
then add you code like
int i = AllRanks.FindIndex(Findlogic);
 
"i" will give you desired index.
  Permalink  
v2
Comments
SAKryukov at 16-May-11 5:39am
   
Correct, but... I voted 3 for quite ridiculous "if".
Should be: "return item == "ABC" || item == "BBC";" (!)
(I also fixed your formatting and added missing ";")
--SA
prathameshpitale at 16-May-11 5:40am
   
Hey thanks. but can you explain me this ?
Albin Abel at 16-May-11 6:34am
   
My 5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

How come you did not see it in System.Collections.Generic.List?
Look:
http://msdn.microsoft.com/en-us/library/6sh2ey19(v=VS.100).aspx[^].
 
You will find a lot of methods including ready-to-use IndexOf (three overloads).
 
Ahh— predicate… sorry, now I see.
 
What's the problem about the code sample here:
http://msdn.microsoft.com/en-us/library/x1xzf2ca.aspx[^]?
 
—SA
  Permalink  
Comments
prathameshpitale at 16-May-11 5:20am
   
I Saw the links before Questioning on this forum.. I have mentioned there i am not able to understand the given methods in example...
 
Can you help me understand the codE?
SAKryukov at 16-May-11 5:36am
   
How can I help you? By writing another piece of code? You won't understand it either.
(However, see the sample by Debojyoti Majumder. Any better?)
Maybe you need to learn about 1) generics, 2) delegates, looks at more simple examples first?
--SA
prathameshpitale at 16-May-11 6:14am
   
Thanx!

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

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 BillWoodruff 220
4 Mathew Soji 195
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,817
2 DamithSL 5,461
3 Manas Bhardwaj 4,946
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 16 May 2011
Copyright © CodeProject, 1999-2014
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