79594346

Date: 2025-04-26 19:58:39
Score: 0.5
Natty:
Report link

Using a for loop and adding items that match the criteria to a new list can often be more performant than LINQ for large lists:

var filteredList = new List<MyObject>();
for (int i = 0; i < myObjects.Count; i++)
{
    var o = myObjects[i];
    if (o.Value > 100 && o.Date < DateTime.Now.AddDays(-7) && SomeComplexCalculation(o) > 50)
    {
        filteredList.Add(o);
    }
}

Consider optimizing SomeComplexCalculation itself for better overall performance. If appropriate, you could potentially pre-calculate results of SomeComplexCalculation and store them as a property on MyObject to avoid repeated expensive computations during filtering.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Brick it