当前位置: 首页 > 编程语言 > .NET综合 > 正文

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

时间:2010-11-04 博客园 lovecherry

select

描述:查询顾客的公司名、地址信息

查询句法:

var 构建匿名类型1 = from c in ctx.Customers
select new
{
公司名 = c.CompanyName,
地址 = c.Address
};

对应SQL:

SELECT [t0].[CompanyName], [t0].[Address]
FROM [dbo].[Customers] AS [t0]

描述:查询职员的姓名和雇用年份

查询句法:

var 构建匿名类型2 = from emp in ctx.Employees
select new
{
姓名 = emp.LastName + emp.FirstName,
雇用年 = emp.HireDate.Value.Year
};

对应SQL:

SELECT [t0].[LastName] + [t0].[FirstName] AS [value], DATEPART(Year, [t0].[HireDate]) AS [value2]
FROM [dbo].[Employees] AS [t0]

描述:查询顾客的ID以及联系信息(职位和联系人)

查询句法:

var 构建匿名类型3 = from c in ctx.Customers
select new
{
ID = c.CustomerID,
联系信息 = new
{
职位 = c.ContactTitle,
联系人 = c.ContactName
}
};

对应SQL:

SELECT [t0].[CustomerID], [t0].[ContactTitle], [t0].[ContactName]
FROM [dbo].[Customers] AS [t0]