> php教程 > php手册 > Linq学习(三)-基本查询

Linq学习(三)-基本查询

WBOY
풀어 주다: 2016-07-06 13:30:25
원래의
1233명이 탐색했습니다.

一、本将主要介绍内容 从linq,sql,lambda三个角度比较来学习 select、orderby、分页、group by、distinct、子查询、in的用法 1.select 查询用户和它们的自我介绍 Linq to sql from a in Blog_UserInfo select new { 真实名字 = a.RealName, 自我介绍 = a.I

一、本将主要介绍内容

从linq,sql,lambda三个角度比较来学习

select、orderby、分页、group by、distinct、子查询、in的用法

1.select

查询用户和它们的自我介绍

Linq to sql

<span style="color: #0000ff">from</span> a <span style="color: #0000ff">in</span><span style="color: #000000"> Blog_UserInfo
</span><span style="color: #0000ff">select</span> <span style="color: #0000ff">new</span><span style="color: #000000">
{
    真实名字</span>=<span style="color: #000000">a.RealName,
    自我介绍</span>=<span style="color: #000000">a.Introduce
}</span>
로그인 후 복사

sql

<span style="color: #0000ff">SELECT</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">RealName</span><span style="color: #ff0000">]</span> <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">真实名字</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">Introduce</span><span style="color: #ff0000">]</span> <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">自我介绍</span><span style="color: #ff0000">]</span>
<span style="color: #0000ff">FROM</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">Blog_UserInfo</span><span style="color: #ff0000">]</span> <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>
로그인 후 복사

Lambda

<span style="color: #000000">Blog_UserInfo
   .Select (
      a </span>=> 
         <span style="color: #0000ff">new</span><span style="color: #000000">  
         {
            真实名字 </span>=<span style="color: #000000"> a.RealName, 
            自我介绍 </span>=<span style="color: #000000"> a.Introduce
         }
   )</span>
로그인 후 복사

2.orderby

查询名字里带friend的用户,并排序

Linq to sql

<span style="color: #0000ff">from</span> a <span style="color: #808080">in</span><span style="color: #000000"> Blog_Users
</span><span style="color: #0000ff">where</span> a.NickName.<span style="color: #0000ff">Contains</span><span style="color: #000000">("Friend")
orderby a.UserId ascending,
a.CreateTime descending
</span><span style="color: #0000ff">select</span><span style="color: #000000"> a
</span><span style="color: #008080">--</span><span style="color: #008080">或者</span>
<span style="color: #0000ff">from</span> a <span style="color: #808080">in</span><span style="color: #000000"> Blog_Users
</span><span style="color: #0000ff">where</span> a.NickName.<span style="color: #0000ff">Contains</span><span style="color: #000000">("Friend")
orderby a.UserId,a.CreateTime 
</span><span style="color: #0000ff">select</span> a
로그인 후 복사

sql

<span style="color: #008080">-- Region</span><span style="color: #008080"> Parameters</span>
<span style="color: #0000ff">DECLARE</span> <span style="color: #008000">@p0</span> <span style="color: #0000ff">NVarChar</span>(<span style="font-weight: bold; color: #800000">1000</span>) <span style="color: #808080">=</span> <span style="color: #ff0000">'</span><span style="color: #ff0000">%Friend%</span><span style="color: #ff0000">'</span>
<span style="color: #008080">-- EndRegion
</span><span style="color: #0000ff">SELECT</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">UserId</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">NickName</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">CreateTime</span><span style="color: #ff0000">]</span>
<span style="color: #0000ff">FROM</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">Blog_User</span><span style="color: #ff0000">]</span> <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>
<span style="color: #0000ff">WHERE</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">NickName</span><span style="color: #ff0000">]</span> <span style="color: #808080">LIKE</span> <span style="color: #008000">@p0</span>
<span style="color: #0000ff">ORDER</span> <span style="color: #0000ff">BY</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">UserId</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">CreateTime</span><span style="color: #ff0000">]</span> <span style="color: #0000ff">DESC</span>
로그인 후 복사

Lambda

<span style="color: #000000">Blog_Users
   .Where (a </span>=> a.NickName.Contains (<span style="color: #800000">"</span><span style="color: #800000">Friend</span><span style="color: #800000">"</span><span style="color: #000000">))
   .OrderBy (a </span>=><span style="color: #000000"> a.UserId)
   .ThenByDescending (a </span>=> a.CreateTime)
로그인 후 복사

3.分页

按照每页2条 ,查询第2页的留言表的信息

Linq to sql

(<span style="color: #0000ff">from</span> a <span style="color: #0000ff">in</span> Blog_LeaveMsgs <span style="color: #0000ff">select</span> a).Skip(<span style="color: #800080">2</span>).Take(<span style="color: #800080">2</span>)
로그인 후 복사

sql

<span style="color: #008080">-- Region</span><span style="color: #008080"> Parameters</span>
<span style="color: #0000ff">DECLARE</span> <span style="color: #008000">@p0</span> <span style="color: #0000ff">Int</span> <span style="color: #808080">=</span> <span style="font-weight: bold; color: #800000">2</span>
<span style="color: #0000ff">DECLARE</span> <span style="color: #008000">@p1</span> <span style="color: #0000ff">Int</span> <span style="color: #808080">=</span> <span style="font-weight: bold; color: #800000">2</span>
<span style="color: #008080">-- EndRegion
</span><span style="color: #0000ff">SELECT</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t1</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">ID</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t1</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">ReceiverId</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t1</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">LeaverId</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t1</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">CreateTime</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t1</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">Content</span><span style="color: #ff0000">]</span>
<span style="color: #0000ff">FROM</span><span style="color: #000000"> (
    </span><span style="color: #0000ff">SELECT</span> ROW_NUMBER() <span style="color: #0000ff">OVER</span> (<span style="color: #0000ff">ORDER</span> <span style="color: #0000ff">BY</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">ID</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">ReceiverId</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">LeaverId</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">CreateTime</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">Content</span><span style="color: #ff0000">]</span>) <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">ROW_NUMBER</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">ID</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">ReceiverId</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">LeaverId</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">CreateTime</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">Content</span><span style="color: #ff0000">]</span>
    <span style="color: #0000ff">FROM</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">Blog_LeaveMsg</span><span style="color: #ff0000">]</span> <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span><span style="color: #000000">
    ) </span><span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t1</span><span style="color: #ff0000">]</span>
<span style="color: #0000ff">WHERE</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t1</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">ROW_NUMBER</span><span style="color: #ff0000">]</span> <span style="color: #808080">BETWEEN</span> <span style="color: #008000">@p0</span> <span style="color: #808080">+</span> <span style="font-weight: bold; color: #800000">1</span> <span style="color: #808080">AND</span> <span style="color: #008000">@p0</span> <span style="color: #808080">+</span> <span style="color: #008000">@p1</span>
<span style="color: #0000ff">ORDER</span> <span style="color: #0000ff">BY</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t1</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">ROW_NUMBER</span><span style="color: #ff0000">]</span>
로그인 후 복사

Lambda

<span style="color: #000000">Blog_LeaveMsgs
   .Select (a </span>=><span style="color: #000000"> a)
   .Skip (</span><span style="color: #800080">2</span><span style="color: #000000">)
   .Take (</span><span style="color: #800080">2</span>)
로그인 후 복사

4.1分组1(group by字段)

根据用户来分组,查询留言数大于等于3条的用户ID和相应留言数量

Linq to sql

<span style="color: #0000ff">from</span> a <span style="color: #0000ff">in</span><span style="color: #000000"> Blog_LeaveMsgs 
group a by a.LeaverId into b
</span><span style="color: #0000ff">where</span> b.Count() >=<span style="color: #800080">3</span>
<span style="color: #0000ff">select</span> <span style="color: #0000ff">new</span><span style="color: #000000">
{
    朋友ID </span>=<span style="color: #000000"> b.Key,
    留言数 </span>=<span style="color: #000000"> b.Count()
}</span>
로그인 후 복사

sql

<span style="color: #008080">-- Region</span><span style="color: #008080"> Parameters</span>
<span style="color: #0000ff">DECLARE</span> <span style="color: #008000">@p0</span> <span style="color: #0000ff">Int</span> <span style="color: #808080">=</span> <span style="font-weight: bold; color: #800000">3</span>
<span style="color: #008080">-- EndRegion
</span><span style="color: #0000ff">SELECT</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t1</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">LeaverId</span><span style="color: #ff0000">]</span> <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">朋友ID</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t1</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">value2</span><span style="color: #ff0000">]</span> <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">留言数</span><span style="color: #ff0000">]</span>
<span style="color: #0000ff">FROM</span><span style="color: #000000"> (
    </span><span style="color: #0000ff">SELECT</span> <span style="color: #ff00ff">COUNT</span>(<span style="color: #808080">*</span>) <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">value</span><span style="color: #ff0000">]</span>, <span style="color: #ff00ff">COUNT</span>(<span style="color: #808080">*</span>) <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">value2</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">LeaverId</span><span style="color: #ff0000">]</span>
    <span style="color: #0000ff">FROM</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">Blog_LeaveMsg</span><span style="color: #ff0000">]</span> <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>
    <span style="color: #0000ff">GROUP</span> <span style="color: #0000ff">BY</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">LeaverId</span><span style="color: #ff0000">]</span><span style="color: #000000">
    ) </span><span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t1</span><span style="color: #ff0000">]</span>
<span style="color: #0000ff">WHERE</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t1</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">value</span><span style="color: #ff0000">]</span> <span style="color: #808080">>=</span> <span style="color: #008000">@p0</span>
로그인 후 복사

4.2分组2(group by多个字段)

按照接收人和留言人进行分组,查看覆盖的接收人和留言人情况

Linq to sql

<span style="color: #0000ff">from</span> a <span style="color: #0000ff">in</span><span style="color: #000000"> Blog_LeaveMsgs
group a by </span><span style="color: #0000ff">new</span><span style="color: #000000">{a.ReceiverId,a.LeaverId} into b
</span><span style="color: #0000ff">select</span> <span style="color: #0000ff">new</span><span style="color: #000000">
{
    接收人ID</span>=<span style="color: #000000">b.Key.ReceiverId,
    留言人ID</span>=<span style="color: #000000">b.Key.LeaverId
}</span>
로그인 후 복사

sql

<span style="color: #0000ff">SELECT</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">ReceiverId</span><span style="color: #ff0000">]</span> <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">接收人ID</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">LeaverId</span><span style="color: #ff0000">]</span> <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">留言人ID</span><span style="color: #ff0000">]</span>
<span style="color: #0000ff">FROM</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">Blog_LeaveMsg</span><span style="color: #ff0000">]</span> <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>
<span style="color: #0000ff">GROUP</span> <span style="color: #0000ff">BY</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">ReceiverId</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">LeaverId</span><span style="color: #ff0000">]</span>
로그인 후 복사

Lambda

<span style="color: #000000">Blog_LeaveMsgs
   .GroupBy (
      a </span>=> 
         <span style="color: #0000ff">new</span><span style="color: #000000">  
         {
            ReceiverId </span>=<span style="color: #000000"> a.ReceiverId, 
            LeaverId </span>=<span style="color: #000000"> a.LeaverId
         }
   )
   .Select (
      b </span>=> 
         <span style="color: #0000ff">new</span><span style="color: #000000">  
         {
            接收人ID </span>=<span style="color: #000000"> b.Key.ReceiverId, 
            留言人ID </span>=<span style="color: #000000"> b.Key.LeaverId
         }
   )</span>
로그인 후 복사

5.distinct

查看留言表中的留言人人数

Linq to sql

(<span style="color: #0000ff">from</span> a <span style="color: #0000ff">in</span><span style="color: #000000"> Blog_LeaveMsgs
</span><span style="color: #0000ff">select</span><span style="color: #000000"> a.LeaverId)
.Distinct()</span>
로그인 후 복사

sql

<span style="color: #0000ff">SELECT</span> <span style="color: #0000ff">DISTINCT</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">LeaverId</span><span style="color: #ff0000">]</span>
<span style="color: #0000ff">FROM</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">Blog_LeaveMsg</span><span style="color: #ff0000">]</span> <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>
로그인 후 복사

Lambda

<span style="color: #000000">Blog_LeaveMsgs
   .Select (a </span>=><span style="color: #000000"> a.LeaverId)
   .Distinct ()</span>
로그인 후 복사

6.子查询

查询留言数量超过4条的用户信息

Linq to sql

<span style="color: #0000ff">from</span> a <span style="color: #0000ff">in</span><span style="color: #000000"> Blog_Users
</span><span style="color: #0000ff">where</span><span style="color: #000000">
(</span><span style="color: #0000ff">from</span> b <span style="color: #0000ff">in</span><span style="color: #000000"> Blog_LeaveMsgs 
group b by b.LeaverId into b 
</span><span style="color: #0000ff">where</span> b.Count()>=<span style="color: #800080">4</span>
<span style="color: #0000ff">select</span><span style="color: #000000"> b.Key).Contains(a.UserId)
</span><span style="color: #0000ff">select</span> a
로그인 후 복사

sql

<span style="color: #008080">-- Region</span><span style="color: #008080"> Parameters</span>
<span style="color: #0000ff">DECLARE</span> <span style="color: #008000">@p0</span> <span style="color: #0000ff">Int</span> <span style="color: #808080">=</span> <span style="font-weight: bold; color: #800000">4</span>
<span style="color: #008080">-- EndRegion
</span><span style="color: #0000ff">SELECT</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">UserId</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">NickName</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">CreateTime</span><span style="color: #ff0000">]</span>
<span style="color: #0000ff">FROM</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">Blog_User</span><span style="color: #ff0000">]</span> <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>
<span style="color: #0000ff">WHERE</span> <span style="color: #808080">EXISTS</span><span style="color: #000000">(
    </span><span style="color: #0000ff">SELECT</span> <span style="color: #0000ff">NULL</span> <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">EMPTY</span><span style="color: #ff0000">]</span>
    <span style="color: #0000ff">FROM</span><span style="color: #000000"> (
        </span><span style="color: #0000ff">SELECT</span> <span style="color: #ff00ff">COUNT</span>(<span style="color: #808080">*</span>) <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">value</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t1</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">LeaverId</span><span style="color: #ff0000">]</span>
        <span style="color: #0000ff">FROM</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">Blog_LeaveMsg</span><span style="color: #ff0000">]</span> <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t1</span><span style="color: #ff0000">]</span>
        <span style="color: #0000ff">GROUP</span> <span style="color: #0000ff">BY</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t1</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">LeaverId</span><span style="color: #ff0000">]</span><span style="color: #000000">
        ) </span><span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t2</span><span style="color: #ff0000">]</span>
    <span style="color: #0000ff">WHERE</span> (<span style="color: #ff0000">[</span><span style="color: #ff0000">t2</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">LeaverId</span><span style="color: #ff0000">]</span> <span style="color: #808080">=</span> (<span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">UserId</span><span style="color: #ff0000">]</span>)) <span style="color: #808080">AND</span> (<span style="color: #ff0000">[</span><span style="color: #ff0000">t2</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">value</span><span style="color: #ff0000">]</span> <span style="color: #808080">>=</span> <span style="color: #008000">@p0</span><span style="color: #000000">)
    )</span>
로그인 후 복사

Lambda

<span style="color: #000000">Blog_Users
   .Where (
      a </span>=><span style="color: #000000"> 
         Blog_LeaveMsgs
            .GroupBy (b </span>=><span style="color: #000000"> b.LeaverId)
            .Where (b </span>=> (b.Count () >= <span style="color: #800080">4</span><span style="color: #000000">))
            .Select (b </span>=><span style="color: #000000"> b.Key)
            .Contains ((Int32</span>?<span style="color: #000000">)(a.UserId))
   )</span>
로그인 후 복사

7.in操作

查询制定用户昵称的用户

Linq to sql

<span style="color: #0000ff">from</span> a <span style="color: #0000ff">in</span><span style="color: #000000"> Blog_Users
</span><span style="color: #0000ff">where</span> <span style="color: #0000ff">new</span> <span style="color: #0000ff">string</span>[]{<span style="color: #800000">"</span><span style="color: #800000">Kimisme</span><span style="color: #800000">"</span>,<span style="color: #800000">"</span><span style="color: #800000">FriendLee</span><span style="color: #800000">"</span><span style="color: #000000">}
.Contains(a.NickName)
</span><span style="color: #0000ff">select</span> a
로그인 후 복사

sql

<span style="color: #008080">-- Region</span><span style="color: #008080"> Parameters</span>
<span style="color: #0000ff">DECLARE</span> <span style="color: #008000">@p0</span> <span style="color: #0000ff">NVarChar</span>(<span style="font-weight: bold; color: #800000">1000</span>) <span style="color: #808080">=</span> <span style="color: #ff0000">'</span><span style="color: #ff0000">Kimisme</span><span style="color: #ff0000">'</span>
<span style="color: #0000ff">DECLARE</span> <span style="color: #008000">@p1</span> <span style="color: #0000ff">NVarChar</span>(<span style="font-weight: bold; color: #800000">1000</span>) <span style="color: #808080">=</span> <span style="color: #ff0000">'</span><span style="color: #ff0000">FriendLee</span><span style="color: #ff0000">'</span>
<span style="color: #008080">-- EndRegion
</span><span style="color: #0000ff">SELECT</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">UserId</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">NickName</span><span style="color: #ff0000">]</span>, <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">CreateTime</span><span style="color: #ff0000">]</span>
<span style="color: #0000ff">FROM</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">Blog_User</span><span style="color: #ff0000">]</span> <span style="color: #0000ff">AS</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>
<span style="color: #0000ff">WHERE</span> <span style="color: #ff0000">[</span><span style="color: #ff0000">t0</span><span style="color: #ff0000">]</span>.<span style="color: #ff0000">[</span><span style="color: #ff0000">NickName</span><span style="color: #ff0000">]</span> <span style="color: #808080">IN</span> (<span style="color: #008000">@p0</span>, <span style="color: #008000">@p1</span>)
로그인 후 복사

Lambda

<span style="color: #000000">Blog_Users
   .Where (a </span>=> <span style="color: #0000ff">new</span> String[] { <span style="color: #800000">"</span><span style="color: #800000">Kimisme</span><span style="color: #800000">"</span>, <span style="color: #800000">"</span><span style="color: #800000">FriendLee</span><span style="color: #800000">"</span> } .Contains (a.NickName))
로그인 후 복사

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿