filtering

  • Thread starter Thread starter newbie
  • Start date Start date
N

newbie

I have a generic list IList<IEmployee>. IEmployee is again a class with
many properties like name, age, etc. I need to do filtering of
employees based on their properties(name, age,...) I can use a foreach
when the number of employees is less in count.... but my employee count
is veryyy huge. Please suggest a feasible way to do filtering.

Thanks
 
Am not using a database here. i need to do filtering using the ilist
itself. i tried using predicate but that doesnt seem to work too...
help me on this. is there some other way? am developing using c#.net.
 
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);
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top