in操作
描述:查询指定城市中的客户
查询句法:
对应SQL:var in操作 = from c in ctx.Customers where new string[] { "Brandenburg", "Cowes", "Stavern" }.Contains(c.City) select c;
joinSELECT [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 [t0].[City] IN (@p0, @p1, @p2) -- @p0: Input String (Size = 11; Prec = 0; Scale = 0) [Brandenburg] -- @p1: Input String (Size = 5; Prec = 0; Scale = 0) [Cowes] -- @p2: Input String (Size = 7; Prec = 0; Scale = 0) [Stavern]
描述:内连接,没有分类的产品查询不到
查询句法:
对应SQL:var innerjoin = from p in ctx.Products join c in ctx.Categories on p.CategoryID equals c.CategoryID select p.ProductName;
描述:外连接,没有分类的产品也能查询到SELECT COUNT(*) AS [value] FROM [dbo].[Products] AS [t0] INNER JOIN [dbo].[Categories] AS [t1] ON [t0].[CategoryID] = ([t1].[CategoryID])
查询句法:
对应SQL:var leftjoin = from p in ctx.Products join c in ctx.Categories on p.CategoryID equals c.CategoryID into pro from x in pro.DefaultIfEmpty() select p.ProductName;
你可能会很奇怪,原先很复杂的SQL使用查询句法会很简单(比如按照条件分组)。但是原先觉得很好理解的SQL使用查询句法会觉得很复杂(比如连接查询)。其实,我们还可以通过其它方式进行连接操作,在以后说DataLoadOptions类型的时候会再说。虽然Linq to sql已经非常智能了,但是对于非常复杂的查询还是建议通过存储过程实现,下次讲解如何调用存储过程。SELECT COUNT(*) AS [value] FROM [dbo].[Products] AS [t0] LEFT OUTER JOIN [dbo].[Categories] AS [t1] ON [t0].[CategoryID] = ([t1].[CategoryID])