I have read that the LINQ is very lazy...
I have a class with a model:
class Movie
{
public string Title { get; set; }
public float Rating { get; set; }
private int year;
public int Year
{
get
{
Console.WriteLine("Returning {0} for movie {1}", year, Title);
return year;
}
set
{
year = value;
}
}
}
And I have the following code:
var movies = new List<Movie>
{
new Movie {Title = "The Dark Knight", Rating = 8.9f, Year = 2008 },
new Movie {Title = "The King's Speech", Rating = 8.0f, Year = 2010 },
new Movie {Title = "Casablanca", Rating = 8.5f, Year = 1942 },
new Movie {Title = "Star Wars 5", Rating = 8.7f, Year = 1980 }
};
var query = movies.Where(m => m.Year > 2000);
Console.WriteLine( query.Count( ) );
foreach(var movie in query)
{
Console.WriteLine(movie.Title);
}
And here is the output:
Returning 2008 for movie The Dark Knight
Returning 2010 for movie The King's Speech
Returning 1942 for movie Casablanca
Returning 1980 for movie Star Wars 5
2
Returning 2008 for movie The Dark Knight
The Dark Knight
Returning 2010 for movie The King's Speech
The King's Speech
Returning 1942 for movie Casablanca
Returning 1980 for movie Star Wars 5
So, the Count method does not make the query to be executed? I see this as an opposite to an optimization... So is there a logical reason behind this? (The only reasonable reason that I see for this behaviour is CQS principle)
Thanks