C# - Efficiently Filtering a Large List of Objects Based on Multiple Complex Criteria

I have a large list of custom objects (List<MyObject>
) in C#, potentially containing millions of items. I need to filter this list based on multiple criteria, some of which involve complex logic or calculations.
public class MyObject
{
public string Name { get; set; }
public int Value { get; set; }
public DateTime Date { get; set; }
// ... other properties ...
}
Currently, I'm using LINQ's Where()
clause with multiple chained conditions, but the performance is becoming a bottleneck as the list grows.
var filteredList = myObjects.Where(o => o.Value > 100 &&
o.Date < DateTime.Now.AddDays(-7) &&
SomeComplexCalculation(o) > 50)
.ToList();
private bool SomeComplexCalculation(MyObject obj)
{
// ... Potentially expensive computation ...
}
What are some more efficient strategies for filtering a large list like this in C#?
I'm looking for solutions that significantly improve performance without sacrificing code readability or maintainability. Clear examples and benchmarks would be greatly appreciated.
Answer
If you could parallelize, one of the obvious choices is Read more
var filteredList = myObjects
.AsParallel()
.Where(o => o.Value > 100 &&
o.Date < DateTime.Now.AddDays(-7) &&
SomeComplexCalculation(o) > 50)
.ToList();
Enjoyed this article?
Check out more content on our blog or follow us on social media.
Browse more articles