Does LINQ-to-SQL Support Composable Queries?

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.

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.

Yes, the resulting query is composed. It includes the full where clause. Turn on SQL profiling and try it to see for yourself.

Linq does this through expression trees. The first linq statement produces an expression tree; it doesn't execute the query. The second linq statement builds on the expression tree created by the first.

The statement is only executed when you enumerate the resulting collection.

Var people = from p in Person where p. Age Age, t0. FirstName FROM dbo.

Person AS t0 WHERE t0. Age PersonId, t0. Age, t0.

FirstName FROM dbo. Person AS t0 WHERE t0. FirstName = @p0 where @p0 gets sent through as "Daniel" var morePeople = from p1 in people from p2 in otherPeople where p1.

PersonId == p2. PersonId select p1; Translates to: SELECT t0. PersonId, t0.

Age, t0. FirstName FROM dbo. Person AS t0, dbo.

Person AS t1 WHERE (t0. PersonId = t1. PersonId) AND (t0.

Age FirstName = @p1) where @p0 is 18, @p1 is "Daniel" When in doubt, call the ToString() on your IQueryable or give a TextWriter to the DataContext's Log property.

People and otherPeople contain objects of type IQueryable. If you iterate over both, separatly, it will run two queries. If you only iterate over otherPeople, it will run the expected query, with two where clauses.

If you do .ToList() on people and use the returned List in the second query instead of people, it becomes LINQ-to-Objects and no SQL is executed. This behavior is referred to as deferred execution. Meaning no query is done until it is needed.

Before execution they are just expression trees that get manipulated to formulate the final query.

Both these queries will be executes when you'll try to access final results. You can try to view original SQL generated from DataContext object properties.

Turn on SQL profiling and try it to see for yourself. Linq does this through expression trees. The first linq statement produces an expression tree; it doesn't execute the query.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions