子查询

Posted on 2010-11-29 22:00  gmtyt  阅读(144)  评论(0)    收藏  举报

子查询

  描述:查询订单数超过5的顾客信息

  查询句法:

 


 var 子查询 = from c in ctx.Customers
  where
  (from o in ctx.Orders group o by o.CustomerID into o where o.Count() > 5 select o.Key).Contains(c.CustomerID)
  select c;

  对应SQL:

 


 SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]
  FROM [dbo].[Customers] AS [t0]
  WHERE EXISTS(
  SELECT NULL AS [EMPTY]
  FROM (
  SELECT COUNT(*) AS [value], [t1].[CustomerID]
  FROM [dbo].[Orders] AS [t1]
  GROUP BY [t1].[CustomerID]
  ) AS [t2]
  WHERE ([t2].[CustomerID] = [t0].[CustomerID]) AND ([t2].[value] > @p0)
  )
  -- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [5]

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3