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

Linq学习(三)-基本查询

WBOY
Freigeben: 2016-07-06 13:30:25
Original
1234 Leute haben es durchsucht

一、本将主要介绍内容 从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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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
Nach dem Login kopieren

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>
Nach dem Login kopieren

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)
Nach dem Login kopieren

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>)
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>)
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

Lambda

<span style="color: #000000">Blog_LeaveMsgs
   .Select (a </span>=><span style="color: #000000"> a.LeaverId)
   .Distinct ()</span>
Nach dem Login kopieren

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
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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
Nach dem Login kopieren

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>)
Nach dem Login kopieren

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))
Nach dem Login kopieren

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage