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,记录一下学习过程以备后续查用. 一.统计单词在字符串中出现的次数 请注意,若要执行计数, ...
随机推荐
- Mongodb学习总结(2)——MongoDB与MySQL区别及其使用场景对比
对于只有SQL背景的人来说,想要深入研究NoSQL似乎是一个艰巨的任务,MySQL与MongoDB都是开源常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数 ...
- CodeForces 367E Sereja and Intervals
CodeForces 3 67E (109 + 7). Two ways are considered distinct if there is such j(1 ≤ j ≤ n), that the ...
- rest frame work纪念版代码
models.py from django.db import models from pygments.lexers import get_all_lexers from pygments.styl ...
- Linux运行级别研究(转)
Linux系统中的运行级别 7种运行级别 运行级别(Runlevel)指的是Unix或者Linux等类Unix操作系统的运行模式,不同的运行模式下系统的功能也有所有不同.Linux 系统下通常分为7种 ...
- CN Internet
来自为知笔记(Wiz)
- 使用SwipeRefreshLayout和RecyclerView实现仿“简书”下拉刷新和上拉载入很多其它
一.概述 本篇博客介绍的是怎样使用SwipeRefreshLayout和RecyclerView实现高仿简书Android端的下拉刷新和上拉载入很多其它的效果. 依据效果图能够发现,本案例实现了例如以 ...
- 纯CSS实现移动端常见布局——高度和宽度挂钩的秘密
纯CSS实现移动端常见布局--高度和宽度挂钩的秘密 不踩坑不回头.之前我在一个项目中大量使用css3的calc计算属性.写代码的时候真心不要太爽啊-可是在项目上线之后,才让我崩溃了,原因非常easy, ...
- LeetCode 690. Employee Importance (职员的重要值)
You are given a data structure of employee information, which includes the employee's unique id, his ...
- 递归,迭代,堆栈三种方式实现单链表反转(C++)
#author by changingivan# 2016.04.12#include <iostream> #include <stack> using namespace ...
- 【bzoj1821】[JSOI2010]Group 部落划分 Group
题目大意:要求把n个点分成m块,使得每一块之间的距离的最小值最大 n^2枚举所有点之间距离 然后sort一下 并查集维护连通关系 一开始e[]开MAXN然后WA了测了4ms,然后开MAXN<&l ...