查找列表项的索引 [英] Finding Index of List Items

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

问题描述

我正在开发一个web scraper应用程序,解析后的HTML进入一个通用List< string> ;.



我想允许搜索功能,但我需要获取找到的项目的索引,以便使用已解析的HTML从其余列表中加载相应的数据。



我的列表可能包含多个重复,但每个实体对应正确的数据,索引的更改或错误的索引将导致其余列表显示错误的数据。



例如 - 我的清单



红色

绿色

红色

红色

红色

红色

红色

绿色

绿色



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

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







^这将返回-1作为th e index。



我该怎么办?



它适用于单个实例但是在一个Foreach循环,它返回值;



0

-1

-1

-1

-1

-1

0

0



等..





希望这有意义并提前感谢你!。




^ 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!.

推荐答案

尝试列表中的IndexOf方法avaialbe。
Try IndexOf method avaialbe in List.


你好,



检查这个链接希望它有所帮助:



如何在C#中获取列表项的索引 [ ^ ]


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

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