您的位置:首页 > 数据库

一步一步学Linq to sql(四):查询句法

2013-04-13 23:07 375 查看
前言

  本次主要来学习并记录一下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]


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: