DbNorthwind northwind = new DbNorthwind();
Console.WriteLine("查询订单数超过5的顾客信息:");
var result =
from c in northwind.Customers
where
(
from o in northwind.Orders
group o by o.CustomerId into o
where o.Count() > 5
select o.Key
).Contains(c.CustomerID)
select c;
Console.WriteLine(northwind.GetCommand(result).CommandText);
Console.WriteLine();
var result =
from c in northwind.Customers
where
(
from o in northwind.Orders
group o by o.CustomerId into o
where o.Count() > 5 and o.CustomerId==c. CustomerID
select o.Key
)
select c;
试试看~
可能是你的o.CustomerId类型不对,我测试了,可以的。
SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]
FROM [Customers] AS [t0]
WHERE EXISTS(
SELECT NULL AS [EMPTY]
FROM (
SELECT [t1].[CustomerID]
FROM [Orders] AS [t1]
GROUP BY [t1].[CustomerID]
) AS [t2]
WHERE ([t2].[CustomerID] = [t0].[CustomerID]) AND (((
SELECT COUNT(*)
FROM [Orders] AS [t3]
WHERE [t2].[CustomerID] = [t3].[CustomerID]
)) > @p0)
)