如何在范围内搜索列表数组 [英] How to search list array with in the range
问题描述
嗨朋友们,
我需要搜索一个有600个项目的数组。我的要求是我需要从索引200到400搜索数组。如何我可以实现吗?
我尝试过的事情:
< pre lang =C#> var lines = File.ReadAllLines(csvpath).Select(x = > x.Split(' ,'))。ToList();
var portdetails = lines.Where(acc = > acc [< span class =code-digit> 0 ]。修剪()== PORTDTLS)。ToList();
var itemsFrom200to400 = lines.Skip( 200 )。取( 200 )。ToList();
var portdetails = itemsFrom200to400.Where(acc = > acc [ 0 ]。修剪()== PORTDTLS)。 ToList();
将所需元素复制到另一个列表(参见 List(T).CopyTo Method(Int32,T [],Int32,Int32)(System.Collections.Generic) [ ^ ])。
我认为这不会导致你真正追求的是什么。
试试这个:
var portdetails = lines.Where(acc = > lines.IndexOf(acc)> = 200 &安培;&安培; lines.IndexOf(acc)< = 400 && acc.Trim()。StartsWith( PORTDTLS)。ToList();
获得行后,然后将它们拆分为逗号分隔符。
另一点 - 指数范围总是200-400?你怎么会知道这事?你怎么能保证呢。我想你需要退一步再看一下这个问题。
Hi friends,
I need to search an array which has 600 items.My requirement is i need to search the array say from index 200 to 400. How can i achieve this?
What I have tried:
var lines = File.ReadAllLines(csvpath).Select(x => x.Split(',')).ToList();
var portdetails = lines.Where(acc => acc[0].Trim() == "PORTDTLS").ToList();
try this
var itemsFrom200to400 = lines.Skip(200).Take(200).ToList(); var portdetails = itemsFrom200to400.Where(acc => acc[0].Trim() == "PORTDTLS").ToList();
Copy the required elements to another list (see List(T).CopyTo Method (Int32, T[], Int32, Int32) (System.Collections.Generic)[^]).
I don't think that's going to result in what you're actually after.
Try this:
var portdetails = lines.Where(acc => lines.IndexOf(acc) >= 200 && lines.IndexOf(acc) <= 400 && acc.Trim().StartsWith("PORTDTLS ").ToList();
After you get the lines, THEN split them on the comma separator.
Another point - is the index range ALWAYS going to be 200-400? How do you know this? How can you guarantee it. I think you need to step back and look at the problem again.
这篇关于如何在范围内搜索列表数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!