且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

LINQ-to-SQL CompiledQuery.Compile() 与更新、删除、插入?

更新时间:2023-01-20 21:30:37

有很大的不同.Linq-To-SQL 选择查询可以是大型复杂表达式树.它的这些可能需要一些时间编译".在这种情况下,合并到一些可以针对 SQL Server 运行的 T-SQL.所以缓存一个操作的结果以便它可以被重用是有意义的.

There is a big difference. Linq-To-SQL select queries can be large complex expressions trees. Its these that may take sometime 'compiling'. In this case coalescing to some T-SQL that can be run against a SQL Server. So it makes sense to cache the result of an operation so that it can be re-used.

然而,其他删除、更新和插入是简单的操作,不需要将表达式树转换为 T-SQL(LINQ 本身就是关于查询的).不幸的是,我们被训练将执行这些其他操作的 SQL 代码视为查询",我们实际上并没有要求任何信息.

However other Delete, Update, and Insert are simple operations that do not require an expression tree to be converted to T-SQL (LINQ itself is all about querying). Its just unfortunate that we've been trained to think of SQL code that performs these other operations as 'queries', we're not actuall asking for any info.

这些操作仅由 DataContext 定义,而不由 LINQ 定义,因此已经编译了执行这些功能的代码.

These operations are only defined by the DataContext not by LINQ hence the code to perform these functions is already compiled.