0

My app has a Collection of Job objects. There is a unique property (Key) called Jobnumber and a DateTime property called Bookingtime which is not necessarily unique. There are various other properties also.

I want to do a lot of linq queries based on the Bookingtime property and occasional inserts and removal of objects from the collection.

If I have 1000 to 2000 objects in a collection should I use a SortedList<TKey, TValue> or just a List<T> and order it manually with linq?

Does the options change for 10,000 objects in the collection?

The objects are from database and are already sorted by Bookingtime but I need to work on certain datetime subsets.

DateTime t1,t2; //equals some values.
var subSet = jobs.where(a=>a.Bookingtime >= t1 &&
a.Bookingtime < =    t2).ToList();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Paul
  • 207
  • 1
  • 3
  • 14

1 Answers1

1

As one can see in the documentation, the SortedList.Add (too bad there is no AddAll method as in Java that could optimize bulk insertion) operation is performed in O(n) whereas OrderBy runs in O(n log n). The implication is that only on small (or sorted lists), the SortedList can outperform Linq.

Furthermore notice that Linq uses lazy evaluation. It will only sort items if you actually need the resulting list (or use a ToList,... method). If you thus later never do anything with the result, the environment won't even sort the data.

This article even implements a truly lazy OrderBy, such that if you only need the first i items, it will not sort the entire list.

EDIT: based on the your updated question, you better incorporate the .where statement as a WHERE in the SQL query. This can reduce network, memory and CPU usage since a database in many cases has means to optimize queries enormously.

Community
  • 1
  • 1
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Sure I do I pull in the 1 months worth of bookings and need to query diffrent subsets of that collection for differing scenarios. – Paul Jan 20 '15 at 14:36
  • I was just wondering if you need a subset of those objects what would be quicker. – Paul Jan 20 '15 at 14:38
  • @user3812046: for such small subsets (and fast tests), I wonder if this will make much difference. Perhaps you should run some benchmarks. – Willem Van Onsem Jan 20 '15 at 14:47