一步一步学Linq to sql(四):查询句法
前言
本次主要来学习并记录一下Linq基本的查询语法。
In 操作
查询指定城市中的客户
var in操作 = from c in ctx.Customers where new string[] { "Brandenburg", "Cowes", "Stavern" }.Contains(c.City) select c;
对应SQL语句
select * from Customers where City in('Brandenburg', 'Cowes', 'Stavern')
查询结果
Join 操作
内连接只能查询出已有分类的物品列表
var innerjoin = from p in ctx.Products join c in ctx.Categories on p.CategoryID equals c.CategoryID select p.ProductName;
对应SQL语句
SELECT [t0].ProductName FROM [dbo].[Products] AS [t0] INNER JOIN [dbo].[Categories] AS [t1] ON [t0].[CategoryID] = ([t1].[CategoryID])
查询结果为
distinct
查询顾客覆盖的国家
var 过滤相同项 = (from c in ctx.Customers orderby c.Country select c.Country).Distinct();
相应的SQL语句
SELECT DISTINCT [t0].[Country] FROM [dbo].[Customers] AS [t0]