Linq查询
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]
描述:查询订单号和订单是否超重的信息
查询句法:
var select带条件 = from o in ctx.Orders
select new
{
订单号 = o.OrderID,
是否超重 = o.Freight > 100 ? "是" : "否"
};
对应SQL:
SELECT [t0].[OrderID],
(CASE
WHEN [t0].[Freight] > @p0 THEN @p1
ELSE @p2
END) AS [value]
FROM [dbo].[Orders] AS [t0]
-- @p0: Input Currency (Size = 0; Prec = 19; Scale = 4) [100]
-- @p1: Input String (Size = 1; Prec = 0; Scale = 0) [是]
-- @p2: Input String (Size = 1; Prec = 0; Scale = 0) [否]
where
描述:查询顾客的国家、城市和订单数信息,要求国家是法国并且订单数大于5
查询句法:
var 多条件 = from c in ctx.Customers
where c.Country == "France" && c.Orders.Count > 5
select new
{
国家 = c.Country,
城市 = c.City,
订单数 = c.Orders.Count
};
对应SQL:
SELECT [t0].[Country], [t0].[City], (
SELECT COUNT(*)
FROM [dbo].[Orders] AS [t2]
WHERE [t2].[CustomerID] = [t0].[CustomerID]
) AS [value]
FROM [dbo].[Customers] AS [t0]
WHERE ([t0].[Country] = @p0) AND (((
SELECT COUNT(*)
FROM [dbo].[Orders] AS [t1]
WHERE [t1].[CustomerID] = [t0].[CustomerID]
)) > @p1)
-- @p0: Input String (Size = 6; Prec = 0; Scale = 0) [France]
-- @p1: Input Int32 (Size = 0; Prec = 0; Scale = 0) [5]
orderby
描述:查询所有没有下属雇员的雇用年和名,按照雇用年倒序,按照名正序
查询句法:
var 排序 = from emp in ctx.Employees
 
补充:Web开发 , ASP.Net ,