Click here to Skip to main content
15,936,046 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am working on a web scraper application and the parsed HTML goes into a generic List<string>.

I want to allow functionality for searches, but I need the to get the index of the found item(s) to load the corresponding data from the rest of the lists with parsed HTML.

My list could contain multiple duplicates but each entity corresponds to the correct data, a change in index or wrong index will cause the rest of the lists to display the wrong data.

e.g - My List

red
green
red
red
red
red
red
green
green

C#
int index = _Colours.FindIndex(x => x.Contains("green"));



^ This Returns -1 as the index.

How can I do this?

It works in a single instance however when in a Foreach loop, it returns values;

0
-1
-1
-1
-1
-1
0
0

etc..


Hope this makes sense and thank you in advance!.
Posted
Updated 5-Aug-13 2:11am
v4
Comments
vinayakJJ 5-Aug-13 8:00am    
what output r u expecting
your code giving correct output to me --> 1 for green
SteveBaldwin21 5-Aug-13 8:09am    
I have updated the question, thank you.

It works in a single instance however when in a Foreach loop, it returns values;

0
-1
-1
-1
-1
-1
0
0

Try IndexOf method avaialbe in List.
 
Share this answer
 
Comments
SteveBaldwin21 5-Aug-13 8:27am    
Thank you, your suggestion did work in a single instance but when in a loop it returns incorrect values.
ArunRajendra 5-Aug-13 11:43am    
can you mark it as answered :)
ArunRajendra 5-Aug-13 11:45am    
There are multiple overloaded method of IndexOf di you try it?
SteveBaldwin21 5-Aug-13 12:44pm    
Just seen this! Didn't realise it had a multiple overload.

Thank you!
ArunRajendra 5-Aug-13 12:47pm    
welcome :)
Hello,

check this link Hope it help :

how to get index of list item in C#[^]
 
Share this answer
 

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