C# Linq for collection has been long in the arena. It promises to make the code clean and minimalistic. Might be slightly less readable when you try to do a lot in less code. But then of comes the big question does it really solve the big issues related to "Green Code", is it really optimized.
When you see a code like below you:
public static int GetLatestVersion(List<Event> events)
{
    int latestVersion = 0;
    foreach (Event event in events)
    {
        if (event.Version > latestVersion)
        {
            latestVersion = event.Version;
        }
    }
    return latestVersion;
}
The first impulse would be to make life easier by switching to linq. Why have so many lines of code for something that can be written as
public static int GetLatestVersion(List<Event> events)
 {
     return events.Max(e => e.Version);
 }
But wait, lets take a moments pause and evaluate the implication. By shifting the loop to linq are we making the code "Greener"? I couldn't find the relevant answer directly from google so decided to investigate myself. 
While linq does make the code simpler, it does add a huge performance overhead. Specially when working in applications that process millions of records in a day the performance impact is significant. There are a few old articles which suggested that performance would be better with newer versions of dotnet, but I don't see we are there yet!!
After multiple trials i see that "Linq" code take 3 times more to find the max value in comparison to our plain old simple "for loop". The larger the data set the more exponential the problem would be. So the old loop wins in comparison to linq.
 

 
    
Top comments (1)
That's good point.
LINQ in C# offers a clean and expressive way to handle collections, often reducing boilerplate code and improving readability. For example, replacing a loop that finds the maximum version in a list with events.Max(e => e.Version) can make the code more concise and declarative. This is especially appealing in scenarios where clarity and maintainability are prioritized. However, LINQ introduces abstraction overhead—such as delegate invocations and potential memory allocations—that can impact performance, particularly in large-scale applications processing millions of records daily.
In performance-critical systems, traditional loops often outperform LINQ by a significant margin. Benchmarks consistently show that a simple foreach loop can be two to three times faster than its LINQ equivalent for operations like finding a maximum value. This difference becomes more pronounced as dataset size increases, making LINQ less suitable for high-throughput environments. While newer versions of .NET have improved LINQ's efficiency, the gap remains noticeable. Developers should weigh the trade-offs between readability and performance, using LINQ judiciously and profiling critical paths to ensure their code stays both elegant and efficient.