Linq学习(三)-基本查询
一、本将主要介绍内容
从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))
Linq学习(三)-基本查询的更多相关文章
- linq学习三个实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- MySql学习(三) —— 子查询(where、from、exists) 及 连接查询(left join、right join、inner join、union join)
注:该MySql系列博客仅为个人学习笔记. 同样的,使用goods表来练习子查询,表结构如下: 所有数据(cat_id与category.cat_id关联): 类别表: mingoods(连接查询时作 ...
- Linq学习<三> linq to entity
之前一直用sql选择出数据放在一个集合中,然后再用Linq或者lambda去操作数据,今天学了Linq to entity 才知道原来linq产生是为了Entity.也就是EDM(实体数据模型) 关于 ...
- Linq学习(四)-联合查询
一.本将主要介绍 Union.Concat.Intersect.Except的使用操作 1.Union 查询昵称中带有Friend和带有Lee的用户 Linq (from a in Blog_User ...
- LinQ实战学习笔记(三) 序列,查询操作符,查询表达式,表达式树
序列 延迟查询执行 查询操作符 查询表达式 表达式树 (一) 序列 先上一段代码, 这段代码使用扩展方法实现下面的要求: 取进程列表,进行过滤(取大于10M的进程) 列表进行排序(按内存占用) 只保留 ...
- Linq学习之旅——LINQ查询表达式
1. 概述 2. from子句 3. where子句 4. select子句 5. group子句 6. into子句 7. 排序子句 8. let子句 9. join子句 10. 小结 1. 概述 ...
- LINQ学习系列-----2.3 迭代器带来的延迟查询
此篇博文承接上一篇博文: LINQ学习系列-----2.2 迭代器 一.第一次执行 废话不多说,上源码: 执行结果下图: 为什么会这样?其实原因很简单 fro ...
- openfire Android学习(三)----会议室创建、加入以及查询会议室中所有成员等
openfire 中的会议室不像QQ群一样,不能保存那些离线用户,加入会议室后,一旦断开连接,就会离开会议室. 虽然如此,但如果要实现也不是不可能,我们可以自己做后台来保存,有兴趣的可以去试着实现一下 ...
- C# LINQ学习笔记三:LINQ to OBJECT之操作字符串
本笔记摘抄自:https://www.cnblogs.com/liqingwen/p/5814204.html,记录一下学习过程以备后续查用. 一.统计单词在字符串中出现的次数 请注意,若要执行计数, ...
随机推荐
- Android BottomSheet:以选取图片为例(2)
Android BottomSheet:以选取图片为例(2) 附录文章5简单介绍了常见的分享面板在BottomSheet中的具体应用.本文再以常见的选取图片为例写一个例子. 布局文件: < ...
- 5、Java并发性和多线程-相同线程
以下内容转自http://tutorials.jenkov.com/java-concurrency/same-threading.html(使用谷歌翻译): 相同线程(同一线程)是一种并发模型,其中 ...
- tomcat这种http服务器,是能接收到客户端的断开信息的,并能打印出来
如,tomcat的运行文件 DEBUG -- CLOSE BY CLIENT STACK TRACE
- lua中的闭包小结
function newCounter() return function() i=i+ return i end end c1=newCounter() print(c1()) print(c1() ...
- Alcatel OmniSwitch 重置密码
OmniSwitch 6250重置密码 Press s to STOP AT MINIBOOT... [Miniboot]->cd "network" value = 0 = ...
- 在全程Linux環境部署IBM Lotus Domino/Notes 8.5
架設藍色巨人的協同合作訊息平台 在全程Linux環境部署IBM Lotus Domino/Notes 8.5 珊迪小姐 坊間幾乎所有探討IBM Domino/Notes的中文書籍,皆是以部署在Micr ...
- 大数据分析:结合 Hadoop或 Elastic MapReduce使用 Hunk
作者 Jonathan Allen ,译者 张晓鹏 Hunk是Splunk公司一款比較新的产品,用来对Hadoop和其他NoSQL数据存储进行探測和可视化,它的新版本号将会支持亚马逊的Elastic ...
- jQyery 整体架构
jQuery的模块 一.jQuery一共有13个模块: 1. 核心方法 2. 回调模块(callbacks) 3. 数据缓存 4. 异步队列(Deffered) 5. 选择器操做 6. 属性操作 7. ...
- 可编程数据平面将OpenFlow扩展至电信级应用(一)
可编程数据平面将OpenFlow扩展至电信级应用(一) 案例:基于WinPath网络处理器的电信极OpenFlow (CG-OF)client实现 作者:Liviu Pinchas, Tao Lang ...
- scikit-learn:matplotlib.pyplot经常使用绘图功能总结(1)
參考:http://matplotlib.org/api/pyplot_api.html 绘图功能总结(2):http://blog.csdn.net/mmc2015/article/details/ ...