基于这个问题: linq中Where和Join的区别是什么?
我的问题如下:
以下两个陈述是否存在性能差异:
from order in myDB.OrdersSet
from person in myDB.PersonSet
from product in myDB.ProductSet
where order.Persons_Id==person.Id && order.Products_Id==product.Id
select new { order.Id, person.Name, person.SurName, product.Model,UrunAd?=product.Name };
Run Code Online (Sandbox Code Playgroud)
和
from order in myDB.OrdersSet
join person in myDB.PersonSet on order.Persons_Id equals person.Id
join product in myDB.ProductSet on order.Products_Id equals product.Id
select new { order.Id, person.Name, person.SurName, product.Model,UrunAd?=product.Name };
Run Code Online (Sandbox Code Playgroud)
我总是会使用第二个因为它更清楚.
我现在的问题是,第一个比第二个慢吗?它是否构建了一个cartesic产品并随后使用where子句对其进行过滤?
谢谢.
这完全取决于您使用的提供商。
使用 LINQ to Objects,它绝对会构建笛卡尔积并随后进行过滤。
对于进程外查询提供程序(例如 LINQ to SQL),这取决于它是否足够智能,能够将其转换为 SQL 连接。即使 LINQ to SQL 不这样做,实际执行查询的查询引擎也可能会这样做 - 您必须检查数据库的相关查询计划工具,以了解实际会发生什么。
旁注:多个“from”子句并不总是产生笛卡尔积 - 一个“from”子句的内容可能取决于较早子句的当前元素,例如
from file in files
from line in ReadLines(file)
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2480 次 |
| 最近记录: |