Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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:

C#
var lines = File.ReadAllLines(csvpath).Select(x => x.Split(',')).ToList();
        
var portdetails = lines.Where(acc => acc[0].Trim() == "PORTDTLS").ToList();

C#

Posted
Updated 3-Oct-16 6:25am

try this
C#
var itemsFrom200to400 = lines.Skip(200).Take(200).ToList();
var portdetails = itemsFrom200to400.Where(acc => acc[0].Trim() == "PORTDTLS").ToList();
 
Share this answer
 
Comments
Philippe Mori 4-Oct-16 9:56am    
Good solution but the first ToList() call is superfluous. Useless memory allocation and waste of time.
Copy the required elements to another list (see List(T).CopyTo Method (Int32, T[], Int32, Int32) (System.Collections.Generic)[^]).
 
Share this answer
 
I don't think that's going to result in what you're actually after.

Try this:

C#
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.
 
Share this answer
 
Comments
jinesh sam 4-Oct-16 4:58am    
Thanks John:) For finding index i have separate method
Philippe Mori 4-Oct-16 10:02am    
I think this solution would be inefficient as it could be... like O(n²) instead of O(n) as I don't believe that Linq to object would optimize such use of IndexOf. Also, if there be duplicate in the list, you might not get expected result as it would probably take the index of the first occurrence of the string instead of the actual one. Thus in OP example, if the same string existing at index 100 and 300, you would probably get neither...
#realJSOP 4-Oct-16 10:12am    
I was merely giving him the answer he was looking for. I'm not concerned with "efficiency" because it's not *my* code. The call to ToList() isn't even necessary, but that's what he was doing, so that's what I did. As for using linq, linq is known to be slower , especially is you start stacking subsequent linq calls on each other. So, you can see that I'm perfectly aware of the "efficiencies", but chose to ignore them.

TECHNICALLY, my answer is appropriate, and does not warrant your 2-vote. BTW, I didn't notice an answer from you for this question - just criticism of the answers that were already here.

Finally, I know exactly what data he is working with because I answered an earlier question of his regarding the same data set.
Philippe Mori 4-Oct-16 10:28am    
Well, I haven't provided a solution 3 + my comment would be my answer...

Although one does not always need to be concerned about efficiency, I do believe in writing code that is reasonably efficient. Thus in a case like that, I really thing that using Skip and Take as it solution 3 worth 2 extra votes.

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