简体   繁体   中英

LINQ Count method : why the query is executed again?

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

Count did executed the query. those are the 4 lines before the "2". the 6 lines after is the foreach. which rerun the query.

If what puzzle you is why the expression was evaluted twice. and by

So, the Count method does not make the query to be executed?

you mean, "Evaluate the conditions and saving the result". this is not how LINQ works.

Generaly speaking, when enumerating LINQ, for each element requested (MoveNext) LINQ is calculating only the new element. This way it doesn't process data that wasn't asked for.

Rerunning the same LINQ expression will go over the elements again and do the processing all over again.

Saving & reusing the result is on you. you can easily do it using ToArray() or ToList()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM