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,记录一下学习过程以备后续查用. 一.统计单词在字符串中出现的次数 请注意,若要执行计数, ...
随机推荐
- BNUOJ 23905 滑雪
滑雪 Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on UESTC. Original ID: 13096 ...
- [POJ3162]Walking Race(DP + 单调队列)
传送门 题意:一棵n个节点的树.wc爱跑步,跑n天,第i天从第i个节点开始跑步,每次跑到距第i个节点最远的那个节点(产生了n个距离),现在要在这n个距离里取连续的若干天,使得这些天里最大距离和最小距离 ...
- Tyvj1139 向远方奔跑(APIO 2009 抢掠计划)
描述 在唐山一中,吃饭是一件很令人头疼的事情,因为你不可能每次都站在队伍前面买饭,所以,你最需要做的一件事就是——跑饭.而跑饭的道路是无比艰难的,因为路是单向的(你要非说成是双向的我也没法,前 ...
- C#中方法的详解
访问修饰符 修饰符 返回值类型 方法名(参数列表){ 语句块;} 访问修饰符:所有类成员访问修饰符都可以使用,如果省略访问修饰符,默认是private. 修饰符:在定义方法时修饰符包括virtual( ...
- [Vue @Component] Control Template Contents with Vue's Render Function
Declaring templates and elements inside of templates works great for most scenarios. Sometimes you n ...
- ZOJ 3675 Trim the Nails(bfs)
Trim the Nails Time Limit: 2 Seconds Memory Limit: 65536 KB Robert is clipping his fingernails. ...
- ios7新增api实现扫描二维码
本来用的ZBar开源库实现的扫描二维码,可是貌似不支持arm64了,也没有在更新. 如今不用适配ios7下面.而iOS新增系统API已支持扫码,參考老外的一篇博客做了个demo.须要的能够參考下 參考 ...
- android_handler(一)
仅仅是一个简单的handler的样例,目的就是对handler有一个初步的接触. 在layout上加入一个button,点击按钮,然后打印出利用handler传送的数据.(都是执行在mainthrea ...
- LeetCode 160. Intersection of Two Linked Lists (两个链表的交点)
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- bzoj3894: 文理分科(还是那道最小割)
3894: 文理分科 题目:传送门 感谢波老师没有来D飞我,让我做出了这题... 题解: 这题其实和我做的上一题(bzoj2132)很像,所以就不写题意了. 依然是那最小割... 这题给出了四个利益矩 ...