一、本将主要介绍内容

从linq,sql,lambda三个角度比较来学习

select、orderby、分页、group by、distinct、子查询、in的用法

1.select

查询用户和它们的自我介绍

Linq to sql

from a in Blog_UserInfo
select new
{
真实名字=a.RealName,
自我介绍=a.Introduce
}

sql

SELECT [t0].[RealName] AS [真实名字], [t0].[Introduce] AS [自我介绍]
FROM [Blog_UserInfo] AS [t0]

Lambda

Blog_UserInfo
.Select (
a =>
new
{
真实名字 = a.RealName,
自我介绍 = a.Introduce
}
)

2.orderby

查询名字里带friend的用户,并排序

Linq to sql

from a in Blog_Users
where a.NickName.Contains("Friend")
orderby a.UserId ascending,
a.CreateTime descending
select a
--或者
from a in Blog_Users
where a.NickName.Contains("Friend")
orderby a.UserId,a.CreateTime
select a

sql

-- Region Parameters
DECLARE @p0 NVarChar(1000) = '%Friend%'
-- EndRegion
SELECT [t0].[UserId], [t0].[NickName], [t0].[CreateTime]
FROM [Blog_User] AS [t0]
WHERE [t0].[NickName] LIKE @p0
ORDER BY [t0].[UserId], [t0].[CreateTime] DESC

Lambda

Blog_Users
.Where (a => a.NickName.Contains ("Friend"))
.OrderBy (a => a.UserId)
.ThenByDescending (a => a.CreateTime)

3.分页

按照每页2条 ,查询第2页的留言表的信息

Linq to sql

(from a in Blog_LeaveMsgs select a).Skip(2).Take(2)

sql

-- Region Parameters
DECLARE @p0 Int = 2
DECLARE @p1 Int = 2
-- EndRegion
SELECT [t1].[ID], [t1].[ReceiverId], [t1].[LeaverId], [t1].[CreateTime], [t1].[Content]
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY [t0].[ID], [t0].[ReceiverId], [t0].[LeaverId], [t0].[CreateTime], [t0].[Content]) AS [ROW_NUMBER], [t0].[ID], [t0].[ReceiverId], [t0].[LeaverId], [t0].[CreateTime], [t0].[Content]
FROM [Blog_LeaveMsg] AS [t0]
) AS [t1]
WHERE [t1].[ROW_NUMBER] BETWEEN @p0 + 1 AND @p0 + @p1
ORDER BY [t1].[ROW_NUMBER]

Lambda

Blog_LeaveMsgs
.Select (a => a)
.Skip (2)
.Take (2)

4.1分组1(group by字段)

根据用户来分组,查询留言数大于等于3条的用户ID和相应留言数量

Linq to sql

from a in Blog_LeaveMsgs
group a by a.LeaverId into b
where b.Count() >=3
select new
{
朋友ID = b.Key,
留言数 = b.Count()
}

sql

-- Region Parameters
DECLARE @p0 Int = 3
-- EndRegion
SELECT [t1].[LeaverId] AS [朋友ID], [t1].[value2] AS [留言数]
FROM (
SELECT COUNT(*) AS [value], COUNT(*) AS [value2], [t0].[LeaverId]
FROM [Blog_LeaveMsg] AS [t0]
GROUP BY [t0].[LeaverId]
) AS [t1]
WHERE [t1].[value] >= @p0

4.2分组2(group by多个字段)

按照接收人和留言人进行分组,查看覆盖的接收人和留言人情况

Linq to sql

from a in Blog_LeaveMsgs
group a by new{a.ReceiverId,a.LeaverId} into b
select new
{
接收人ID=b.Key.ReceiverId,
留言人ID=b.Key.LeaverId
}

sql

SELECT [t0].[ReceiverId] AS [接收人ID], [t0].[LeaverId] AS [留言人ID]
FROM [Blog_LeaveMsg] AS [t0]
GROUP BY [t0].[ReceiverId], [t0].[LeaverId]

Lambda

Blog_LeaveMsgs
.GroupBy (
a =>
new
{
ReceiverId = a.ReceiverId,
LeaverId = a.LeaverId
}
)
.Select (
b =>
new
{
接收人ID = b.Key.ReceiverId,
留言人ID = b.Key.LeaverId
}
)

5.distinct

查看留言表中的留言人人数

Linq to sql

(from a in Blog_LeaveMsgs
select a.LeaverId)
.Distinct()

sql

SELECT DISTINCT [t0].[LeaverId]
FROM [Blog_LeaveMsg] AS [t0]

Lambda

Blog_LeaveMsgs
.Select (a => a.LeaverId)
.Distinct ()

6.子查询

查询留言数量超过4条的用户信息

Linq to sql

from a in Blog_Users
where
(from b in Blog_LeaveMsgs
group b by b.LeaverId into b
where b.Count()>=4
select b.Key).Contains(a.UserId)
select a

sql

-- Region Parameters
DECLARE @p0 Int = 4
-- EndRegion
SELECT [t0].[UserId], [t0].[NickName], [t0].[CreateTime]
FROM [Blog_User] AS [t0]
WHERE EXISTS(
SELECT NULL AS [EMPTY]
FROM (
SELECT COUNT(*) AS [value], [t1].[LeaverId]
FROM [Blog_LeaveMsg] AS [t1]
GROUP BY [t1].[LeaverId]
) AS [t2]
WHERE ([t2].[LeaverId] = ([t0].[UserId])) AND ([t2].[value] >= @p0)
)

Lambda

Blog_Users
.Where (
a =>
Blog_LeaveMsgs
.GroupBy (b => b.LeaverId)
.Where (b => (b.Count () >= 4))
.Select (b => b.Key)
.Contains ((Int32?)(a.UserId))
)

7.in操作

查询制定用户昵称的用户

Linq to sql

from a in Blog_Users
where new string[]{"Kimisme","FriendLee"}
.Contains(a.NickName)
select a

sql

-- Region Parameters
DECLARE @p0 NVarChar(1000) = 'Kimisme'
DECLARE @p1 NVarChar(1000) = 'FriendLee'
-- EndRegion
SELECT [t0].[UserId], [t0].[NickName], [t0].[CreateTime]
FROM [Blog_User] AS [t0]
WHERE [t0].[NickName] IN (@p0, @p1)

Lambda

Blog_Users
.Where (a => new String[] { "Kimisme", "FriendLee" } .Contains (a.NickName))

最新文章

  1. [机器学习] 虚拟机VMware中使用Ubuntu的联网问题
  2. 【原创】(AMD)JavaScript模块化开发(dojo)
  3. [JSOI2008][BZOJ1012] 最大数(动态开点线段树)
  4. Sprint第三个冲刺(第三天)
  5. hdu 1021 Fibonacci Again(找规律)
  6. cocos2d-x 2.2.6中c++通过JNI与java互调
  7. centos6 + tomcat+ jdk配置步骤
  8. C++新建Dialog程序
  9. MongoDB入门学习笔记之简介与安装配置
  10. [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数
  11. 抽象工厂模式(Abstract Factory Pattern)
  12. is not in the sudoers file解决方法
  13. _spellmod_on_learn、_spellmod_on_remove
  14. MATLAB 编辑器和程序调试
  15. ios10系统以下原生传来的base64图片无法转化为二进制
  16. .net下将exe改造为既能双击独立运行又能注册为windows服务
  17. Form表单元素
  18. js:深入函数的定义
  19. Aizu 2456 Usoperanto (贪心)
  20. bzoj2730矿场搭建——点双连通分量

热门文章

  1. HDU 1059 多重背包问题
  2. 有一张表里面有上百万的数据,在做查询的时候,如何优化?从数据库端,java端和查询语句上回答
  3. 夜话JAVA设计模式之代理模式(Proxy)
  4. [codevs 1961]躲避大龙(dfs)
  5. HDFS2.0之简单总结
  6. java的异常(Exception)信息的详细记录
  7. QT如何修改字符编码格式
  8. 解决多个Xcode导致的N个模拟器的问题
  9. Java泛型解析(01):认识泛型
  10. [Unity UGUI]ScrollRect效果大全