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.