Optimizing custom collection for access?

  • Thread starter Thread starter Mel
  • Start date Start date
M

Mel

I have a large collection of custom objects, each representing a period in
time with each having a start datetime and an end datetime. I frequently
need to query this collection to return a subset of the objects that fall
completely or partially between two specified dates. The way I'm doing this
at the moment is to iterate thru the entire collection on each query and
pull out the valid objects, but this is hardly an optimal way to do it. How
can I set up my collection to make this sort of operation most efficient?

Thx
 
The only way to filter a collection of objects based on specified criteria
is to loop through all the objects the way you are. The only alternative
would be to provide a static class that accepts certain parameters, does the
filtering at your database (I'm assuming you are using one here) and then
return a collection based on those return results.

The expensive part of your taks is the searching, something SQL was designed
for.

If you are not using a database then your best approach I would think would
be to optimize your looping logic.

MattC
 
Mel,

In addition to what MattC said, you might want to consider storing the
details of each object in a data set, and then having the collection hold
the dataset itself. The objects themselves can know what their ids are, and
then just have a reference to the parent data set, pulling out the details
when needed (searching themselves on the unique id).

Once you do that, you can then use the filtering abilities of the
dataview class (or the Select method on the data table class) to filter your
queries by a certain criteria.

Hope this helps.
 
Mel said:
I have a large collection of custom objects, each representing a period in
time with each having a start datetime and an end datetime. I frequently
need to query this collection to return a subset of the objects that fall
completely or partially between two specified dates. The way I'm doing this
at the moment is to iterate thru the entire collection on each query and
pull out the valid objects, but this is hardly an optimal way to do it. How
can I set up my collection to make this sort of operation most efficient?

Thx

Hi,

Isn't it possible to sort the collection in some way, and perform some
binary searching on it, to (roughly or exactly) find the first and last
valid object. This might give you a smaller subrange of objects for
further evaluation.

Cheers,
Benoit.
 
Benoit,

Once you have sorted the data and as you say found the first an last valid
objects could you simply then just create a new collection using the objects
between the indexes found?

Is this what you mean?

MattC
 
Thanks guys. There seems to be no obvious/easy way of improving efficiency
that I was overlooking. Probably the best approach would be to sort the
collection in chrono order and implement binary searches for both the start
and end items in the sought-after range. For now I'll just leave it as it is
and revisit it if need be.
 
Nicholas,

I've been toying with this idea on my current BLL object collections. It
seems a good idea on the whole. However, how would you suggest persisting
the DataSet over postbacks when used in an ASP.NET application. Would not
placing the Collection and DataSet in the Session/ViewState be expensive? I
guess it comes down to the Time to load by recreating the DataSet with DB
call versus the memory usage hit and serialisation/deserialisation of
Session/ViewState.

Any thoughts.

TIA

MattC

Nicholas Paldino said:
Mel,

In addition to what MattC said, you might want to consider storing the
details of each object in a data set, and then having the collection hold
the dataset itself. The objects themselves can know what their ids are,
and then just have a reference to the parent data set, pulling out the
details when needed (searching themselves on the unique id).

Once you do that, you can then use the filtering abilities of the
dataview class (or the Select method on the data table class) to filter
your queries by a certain criteria.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mel said:
I have a large collection of custom objects, each representing a period in
time with each having a start datetime and an end datetime. I frequently
need to query this collection to return a subset of the objects that fall
completely or partially between two specified dates. The way I'm doing
this at the moment is to iterate thru the entire collection on each query
and pull out the valid objects, but this is hardly an optimal way to do
it. How can I set up my collection to make this sort of operation most
efficient?

Thx
 
Back
Top