Remove Elements from a list

kaizen

Member
Joined
Mar 12, 2008
Messages
8
Programming Experience
5-10
Hi,

I have a List of a custom class (barData) that I need to remove elements form based on a date.

The bardata class holds data of stock price information.

PriceDate
Open
High
Low
Close
Volume

I have
VB.NET:
Public QuoteData As New List(Of BarData)()

Which then gets loaded with data for each day for the stock selected.

What I need to do is remove some data based on a start and end date.

Is there a way to do it without looping through each bar as I may have 2000 plus bars for each stock and may have 2000+ plus stocks to look at. It can be done by looping but it is slow.

Thanks.
 
The is no way to check all elements without looping through them, but it can be done by simpler code employing Linq:
VB.NET:
quoteData = (From x In quoteData Where x.PriceDate < startDate OrElse x.PriceDate > endDate).ToList
If you don't actually need to get a new final list that contains the query selection you can do better using the In-Memory Query directly like this:
VB.NET:
Dim selection = From x In QuoteData Where x.PriceDate < startDate OrElse x.PriceDate > endDate
For Each match In selection

Next
'selection' variable here points to the enumerable Linq query of the collection, not the complete result of the query. That means as you iterate the selection the quoteData collection will be processed from one match to the other live. That also means the first result will be available almost immediately without waiting for the whole collection to be processed.
 
Back
Top