一、本将主要介绍内容

从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学习(三)-基本查询的更多相关文章

  1. linq学习三个实例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. MySql学习(三) —— 子查询(where、from、exists) 及 连接查询(left join、right join、inner join、union join)

    注:该MySql系列博客仅为个人学习笔记. 同样的,使用goods表来练习子查询,表结构如下: 所有数据(cat_id与category.cat_id关联): 类别表: mingoods(连接查询时作 ...

  3. Linq学习<三> linq to entity

    之前一直用sql选择出数据放在一个集合中,然后再用Linq或者lambda去操作数据,今天学了Linq to entity 才知道原来linq产生是为了Entity.也就是EDM(实体数据模型) 关于 ...

  4. Linq学习(四)-联合查询

    一.本将主要介绍 Union.Concat.Intersect.Except的使用操作 1.Union 查询昵称中带有Friend和带有Lee的用户 Linq (from a in Blog_User ...

  5. LinQ实战学习笔记(三) 序列,查询操作符,查询表达式,表达式树

    序列 延迟查询执行 查询操作符 查询表达式 表达式树 (一) 序列 先上一段代码, 这段代码使用扩展方法实现下面的要求: 取进程列表,进行过滤(取大于10M的进程) 列表进行排序(按内存占用) 只保留 ...

  6. Linq学习之旅——LINQ查询表达式

    1. 概述 2. from子句 3. where子句 4. select子句 5. group子句 6. into子句 7. 排序子句 8. let子句 9. join子句 10. 小结 1. 概述 ...

  7. LINQ学习系列-----2.3 迭代器带来的延迟查询

    此篇博文承接上一篇博文: LINQ学习系列-----2.2 迭代器 一.第一次执行                      废话不多说,上源码: 执行结果下图: 为什么会这样?其实原因很简单 fro ...

  8. openfire Android学习(三)----会议室创建、加入以及查询会议室中所有成员等

    openfire 中的会议室不像QQ群一样,不能保存那些离线用户,加入会议室后,一旦断开连接,就会离开会议室. 虽然如此,但如果要实现也不是不可能,我们可以自己做后台来保存,有兴趣的可以去试着实现一下 ...

  9. C# LINQ学习笔记三:LINQ to OBJECT之操作字符串

    本笔记摘抄自:https://www.cnblogs.com/liqingwen/p/5814204.html,记录一下学习过程以备后续查用. 一.统计单词在字符串中出现的次数 请注意,若要执行计数, ...

随机推荐

  1. idea 如何将本地代码上传到github

    1. 首先切换到项目根目录下 执行 git init 2. 点击项目右键->Git->Repository->Remotes->编辑URL 到github代码地址.

  2. Codeforces Round #544 (Div. 3) Editorial C. Balanced Team

    http://codeforces.com/contest/1133/problem/Ctime limit per test 2 secondsmemory limit per test 256 m ...

  3. maven 本地配置

    1.安装 配置环境变量 文件路径:C:\Program Files\apache-maven-3.3.9 2.本地文件配置 setting的配置 找到文件C:\Program Files\apache ...

  4. Linux下使用tcpdump进行抓包(转)

    技巧: 1.可以通过tcpdump抓取某个网卡的包,然后输出日志文件,通过Wireshark进行分析. 2.可以设置Wifi热点,然后通过手机连接这个热点,然后进行tcpdump的分析.而且在Ubun ...

  5. 重学数据结构系列之——平衡树之SB Tree(Size Blanced Tree)

    学习来源:计蒜客 平衡树 1.定义 对于每一个结点.左右两个子树的高度差的绝对值不超过1,或者叫深度差不超过1 为什么会出现这样一种树呢? 假如我们依照1-n的顺序插入到二叉排序树中,那么二叉排序树就 ...

  6. openCV—Python(2)—— 载入、显示和保存图像

    一.函数简单介绍 1.imread-读取图像 函数原型:imread(filename, flags=None) filename:读取的图像路径名:比如:"H:\img\lena.jpg& ...

  7. Mysql常用索引及优化

    索引是帮助我们快速获取数据的数据结构.索引是在存储引擎中实现的,因此不同存储引擎的索引也不同.这里只介绍InnoDB存储索引所支持的BTree索引: 一.索引类型 为了方便举例子,先创建表person ...

  8. mac 下安装Anaconda Python

    # 将anaconda的bin目录加入PATH echo 'export PATH="/Users/work/anaconda/bin/:$PATH"' >> ~/.b ...

  9. CodeForces 16B Burglar and Matches(贪心)

    B. Burglar and Matches time limit per test 0.5 second memory limit per test 64 megabytes input stand ...

  10. 【bzoj1002】 [FJOI2007]轮状病毒DP

    递推+环状特殊处理+高精度   #include<algorithm> #include<iostream> #include<cstdlib> #include& ...