We know that a LINQ to SQL query is not executed inside your C# program. Instead, it is translated into SQL, sent across a wire, and executed on a database server. Let's say we have the following LINQ Query
var query = from c in db.Customers
where c.City == "Nantes"
select new { c.City, c.CompanyName };
It is first translated into the following SQL statement and then executed on a database server:
SELECT [t0].[City], [t0].[CompanyName]
FROM [dbo].[Customers] AS [t0]
WHERE [t0].[City] = @p0
My question is, why not let the compiler directly compile LINQ Query into SQL, why convert LINQ query into a Expression tree first then "compile" this Expression tree into SQL, which adds an extra layer?
I understand the there is other database technologies such as no sql etc, so we can't let the C# complier to handle every different technologies, so does it mean that it is the database provider such as Microsoft.EntityFrameworkCore.SqlServer will analyse Expression Tree and turn it into SQL, which means that it is the database provider that handle Expression Tree?
CodePudding user response:
That is correct. Expression tree is a kind of AST (Abstract syntax tree) which needs to be processed by the "provider" like SQL Server or PostgreSQL or NoSQL, or even XML.