So that we know what we are talking about, can we define "veryyy
huge"... even working with a few hundred thousand should be fast
enough, as long as you caching the results and not re-querying it
every time... the List<T>.FindAll approach is quite tidy here. The
other factor is the performance of your query; assuming this isn't too
complex it should be fine.
Another aspect is "indexing" - in the loose sense. For instance, if
you are likely to be often querying data in a range on a certain
property, and not doing many updates, then a SortedList may be
useful - you can then at least stop looking once you get past the
range... (and there are also tricks to get a good start pos).
Predicates are *marginally* slower than straight code, but possibly
more versatile if you need to do ad-hoc permutations.
However: all of this is relative: the following sample takes 90ms (in
debug mode, with debugger attached) to find all 46k employees (from
1M) born between 98 and 00; to my mind, 90ms is OK in this scenario.
So how many employees are we talking about here? And what sort of
performance are you after?
Marc
using System;
using System.Collections.Generic;
public class Employee
{
private readonly DateTime _dob;
public DateTime DoB { get { return _dob; } }
public Employee(DateTime dob)
{
_dob = dob.Date;
}
}
class Program
{
static void Main()
{
// invent some data
const int COUNT = 1000000;
List<Employee> emps = new List<Employee>(COUNT);
Random rand = new Random(123456);
DateTime epoc = new DateTime(1960,1,1);
for (int i = 0; i < COUNT; i++) {
emps.Add(new Employee(epoc.AddDays(rand.Next(16000))));
}
DateTime queryStart = new DateTime(1998, 1, 1),
qeuryEnd = new DateTime(2000, 1, 1);
DateTime start = DateTime.Now;
List<Employee> results = emps.FindAll(delegate(Employee emp) {
return emp.DoB >= queryStart && emp.DoB < qeuryEnd;
});
// query it
DateTime stop = DateTime.Now;
Console.WriteLine("{0}: {1}", results.Count, (stop -
start).TotalMilliseconds);
}
}