Accepted answer

You can speed up that specific statement you emphasized

cas.Where(x => x.Datetime >= min && x.Datetime <= max).ToList();

With binary search over cas list. First pre-sort cas by Datetime:

cas.Sort((a,b) => a.Datetime.CompareTo(b.Datetime));

Then create comparer for Record which will compare only Datetime properties (implementation assumes there are no null records in the list):

private class RecordDateComparer : IComparer<Record> {
    public int Compare(Record x, Record y) {
        return x.Datetime.CompareTo(y.Datetime);

Then you can translate your Where clause like this:

var index = cas.BinarySearch(new Record { Datetime = min }, new RecordDateComparer());
if (index < 0)
    index = ~index;
var possibleResults = new List<Record>();    
// go backwards, for duplicates            
for (int i = index - 1; i >= 0; i--) {
    var res = cas[i];
    if (res.Datetime <= max && res.Datetime >= min)
    else break;
// go forward until item bigger than max is found
for (int i = index; i < cas.Count; i++) {
    var res = cas[i];
    if (res.Datetime <= max &&res.Datetime >= min)
    else break;

Idea is to find first record with Datetime equal or greater to your min, with BinarySearch. If exact match is found - it returns index of matched element. If not found - it returns negative value, which can be translated to the index of first element greater than target with ~index operation.

When we found that element, we can just go forward the list and grab items until we find item with Datetime greater than max (because list is sorted). We need to go a little backwards also, because if there are duplicates - binary search will not necessary return the first one, so we need to go backwards for potential duplicates.

Additional improvements might include:

  • Putting active codes in a Dictionary (keyed by Value) outside of for loop, and thus replacing codes Where search with Dictionary.ContainsKey.

  • As suggested in comments by @Digitalsa1nt - parallelize foreach loop, using Parallel.For, PLINQ, or any similar techniques. It's a perfect case for parallelization, because loop contains only CPU bound work. You need to make a little adjustments to make it thread-safe of course, such as using thread-safe collection for errors (or locking around adding to it).


There a few improvements you can make here. It might only be a minor performance increase but you should try using groupby instead of where in this circumstance.

So instead you should have something like this:

cas.GroupBy(x => x.DateTime >= min && x.DateTime <= max).Select(h => h.Key == true);

This ussually works for seaching through lists for distinct values, but in you case I'm unsure if it will provide you any benefit when using a clause.

Also a few other things you can do throughout you code:

  • Avoid using ToList when possible and stick to IEnumerable. ToList performs an eager evaluation which is probably causing a lot of slowdown in your query.
  • use .Any() instead of Count when checking if values exist (This only applies if the list is IEnumerable)


Try adding AsNoTracking in the list

The AsNoTracking method can save both execution times and memory usage. Applying this option really becomes important when we retrieve a large amount of data from the database.

var possibleResults = cas.Where(x => x.Datetime >= min && x.Datetime <= max).AsNoTracking().ToList(); //around 4,6599ms

Related Query

More Query from same tag