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))
时间: 2024-12-19 04:26:03

Linq学习(三)-基本查询的相关文章

linq学习三个实例

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LinqExer2 { class Program { static void Main(string[] args) { //1.LINQ实例一 //int[] number = { 2, 4, 3, 5, 7, 21, 34 }; //var

Linq学习(四)-联合查询

一.本将主要介绍 Union.Concat.Intersect.Except的使用操作 1.Union 查询昵称中带有Friend和带有Lee的用户 Linq (from a in Blog_Users where a.NickName.Contains("Lee") select a) .Union (from a in Blog_Users where a.NickName.Contains("Friend") select a) sql -- Region P

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

同样的,使用goods表来练习子查询,表结构如下: 所有数据(cat_id与category.cat_id关联): 类别表: mingoods(连接查询时作测试) 一.子查询 1.where型子查询:把内层查询的结果作为外层查询的比较条件 1.1 查询id最大的一件商品(使用排序+分页实现) :mysql> SELECT goods_id,goods_name,shop_price FROM goods ORDER BY goods_id DESC LIMIT 1; 1.2 查询id最大的一件商

Entity Framework学习三:查询、插入、更新和删除操作

1.LINQ过滤数据  var query = from person in context.People where person.FirstName.StartsWith("a") select person; var methodQuery = context.People.Where(p => p.FirstName.StartsWith("a")); 两种不同的写法,效果一样. 多条件组合查找 var query = from person in c

Linq学习日记-模糊查询

n.Name.StartsWith(Name)--相当于A%n.Name.EndsWith(Name)  --相当于%An.Name.IndexOf(Name)!=-1 --相当于%A%var listWhere = list.Where(n=>n.Name.StartsWith(Name)||n.Name.EndsWith(Name)||n.Name.IndexOf(Name)!=-1);或var listWhere = list.Where(n=>n.Name.Contains(Name)

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

1. 概述 2. from子句 3. where子句 4. select子句 5. group子句 6. into子句 7. 排序子句 8. let子句 9. join子句 10. 小结 1. 概述 LINQ的全称是Language Integrated Query,中文译成“语言集成查询”.LINQ作为一种查询技术,首先要解决数据源的封装,大致使用了三大组件来实现这个封装,分别是LINQ to Object.LINQ to ADO.NET.LINQ to XML.它们和.NET语言的关系如下:

Linq三种查询

using System; using System.Collections.Generic; using System.Linq; using System.Text; /* * 1.Linq查询结果有两种类型:一个是枚举,一个是标量(scalar) */ namespace Linq三种查询 { class Program { static void Main(string[] args) { int[] numbers = { 12,23,34,45,56,67,78,89}; var n

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

此篇博文承接上一篇博文: LINQ学习系列-----2.2 迭代器 一.第一次执行                      废话不多说,上源码: 执行结果下图: 为什么会这样?其实原因很简单 from n in intArray select Square(n) 可以翻译为:Enumerable.Select<int,double>(intArray,n=>Square(n)); 看过上一篇文章的基本信息知道一些了,Enumerable.Select就是个迭代器,这也是延迟查询的奥秘.

LINQ驱动数据的查询功能

一.LINQ概念 LINQ是微软在.NetFramework3.5中新加入的语言功能,在语言中以程序代码方式处理集合的能力. 1.1 LINQ VS 循环处理 在我刚工作时候,对于集合对象的处理一般是采用循环这个集合处理,在处理实值类型上这样做效率还行,但是如果集合内是引用类型,使用LINQ就方便的多,例如一个程序要计算课程的总分和平均分,实体分别是Student和StudentScore 类的申明如下: /// <summary> /// 学生实体 /// </summary>