Speaking as a non-C# savvy programmer, I’m curious as to the evaluation semantics of LINQ queries like the following:
var people = from p in Person where p.age < 18 select p var otherPeople = from p in people where p.firstName equals 'Daniel' select p
Assuming that Person is an ADO entity which defines the age and firstName fields, what would this do from a database standpoint? Specifically, would the people query be run to produce an in-memory structure, which would then be queried by the otherPeople query? Or would the construction of otherPeople merely pull the data regarding the query from people and then produce a new database-peered query? So, if I iterated over both of these queries, how many SQL statements would be executed?
They are composable. This is possible because LINQ queries are actually expressions (code as data), which LINQ providers like LINQ-to-SQL can evaluate and generate corresponding SQL.
Because LINQ queries are lazily evaluated (e.g. won’t get executed until you iterate over the elements), the code you showed won’t actually touch the database. Not until you iterate over otherPeople or people will SQL get generated and executed.