在这个情况下:
var allCustomers = from c in customers select c;
var oldCustomers = from o in allCustomers where o.age > 70 select o;
where子句会到达数据库吗?
在这个情况下:
var allCustomers = from c in customers select c;
var oldCustomers = from o in allCustomers where o.age > 70 select o;
where子句会到达数据库吗?
我想你的意思是:
var oldCustomers = from o in allCustomers where o.age > 70 select o;
是的,它将到达数据库。
尝试使用 LINQPad 查看生成的 SQL 代码。这是一个例子:
我有一个Actor
包含以下字段的表:
Id | Name | Age
编码:
var x = from c in Actors where c.Name.Contains("a") select c;
var y = from c in x where c.Age > 0 select c;
被翻译成:
-- Region Parameters
DECLARE @p0 Int = 0
DECLARE @p1 NVarChar(3) = '%a%'
-- EndRegion
SELECT [t0].[Id], [t0].[Name], [t0].[Age]
FROM [Actor] AS [t0]
WHERE ([t0].[Age] > @p0) AND ([t0].[Name] LIKE @p1)
所以你可以看到它是如何将两个查询混合在一个查询中的。
请记住,IEnumerables 是惰性的,因此除非必须知道它们的元素(因为您遍历它们,或者因为您确实.Count()
要查看有多少项等),否则它不会执行任何查询/操作。