iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 11
0
Modern Web

零經驗ASP .NET Core 30 DAY全紀錄系列 第 11

零經驗 .NET Core 30 DAY----- Day11 LINQ (JOIN、OrderBy、ThenBy)

大家晚安,轉職談的還不錯,不過確切內容還要等上工以後才知道是做開發還是維運,期許我明年可以把新工作的筆記整理好,再參加一次鐵人賽囉/images/emoticon/emoticon08.gif
昨天,我用LIST排序比對,來做座位表的顯示,今天修正用JOIN、OrderBy、ThenBy來做這件事情。

JOIN用法

using (var ctx = new DBContext()){
    var result = (from a in ctx.Table1
    join b in ctx.Table2 on
    new {  a.key欄位1,  a.key欄位2 } equals 
    new {  b.key欄位1, b.key欄位}
    select new{
        a.要select的欄位1,
         b. 要select的欄位2
    }).ToList();
}

OrderBy用法

using (var ctx = new DBContext()){
    var result  = ctx.Table.Select(e => new { e.欄位1, e. 欄位2, e. 欄位3} ).OrderBy(e=>e.排序依據的欄位).ToList();
}

ThenBy用法

using (var ctx = new DBContext()){
    var result  = ctx.Table.Select(e => new { e.欄位1, e. 欄位2, e. 欄位3} ).OrderBy(e=>e.第一排序依據的欄位) .ThenBy(a => a.第二順位排序的欄位).ToList();
}

Descending用法(ThenByDescending,OrderByDescending為遞減排序)

using (var ctx = new DBContext()){
    var result  = ctx.Table.Select(e => new { e.欄位1, e. 欄位2, e. 欄位3} ).OrderByDescending (e=>e.第一排序依據的欄位) .ThenByDescending (a => a.第二順位排序的欄位).ToList();
}

實際應用
SQL語法
select a.T_id,a.T_x,a.T_y,a.S_name,b.T_StatusId,b.T_StatusColor,b.T_StatusName from Table_Detail a JOIN Table_Status b on a.T_StatusId = b.T_StatusId where a.T_id = 1 ORDER by a.T_x,a.T_y;
結果:
https://ithelp.ithome.com.tw/upload/images/20200917/20130030RohCTboXng.png
controller中LINQ寫法

var T_id = ctx.TableAll.Where(n=>n.TName==T).Select(n=>n.TId).ToList();  
var result = (from a in ctx.TableDetail
     join al in ctx.TableStatus on
     new { a.TId, a.TStatusId } equals 
     new { al.TId,  al.TStatusId}
     select new
     {
         a.TId,
         a.TX,
         a.TY,
         a.SName,
         a.TStatusId,
         al.TStatusName,
         al.TStatusColor,
     }).Where(a=> a.TId == T_id[0]).OrderBy(a => a.TY).ThenBy(a => a.TX).ToList();

.cshtml

@{
int[] TX = new int[ViewBag.TX.Count];
ViewBag.TX.CopyTo(TX);
int[] TY = new int[ViewBag.TY.Count];
ViewBag.TY.CopyTo(TY);
int[] TStatusId = new int[ViewBag.TStatusId.Count];
ViewBag.TStatusId.CopyTo(TStatusId);
String[] SeatNAME = new String[@ViewBag.SeatNAME.Count];
ViewBag.SeatNAME.CopyTo(SeatNAME);
String[] SName = new String[@ViewBag.SName.Count];
ViewBag.SName.CopyTo(SName);
String[] SColor = new String[@ViewBag.SColor.Count];
ViewBag.SColor.CopyTo(SColor);
}

<section id="three" class="wrapper style2 special">
<!---->
<div style = "width:80%;margin:0px auto">
<table id="dtable">
    @{int t1 = ViewBag.TX.Count;
    int t2 = ViewBag.TY.Count;
    //Console.Write(t1);
    //Console.Write(t2);
    int C = TX[t1-1];
    int R = TY[t2-1];
    int z = 0;
    for(int i = 1; i <= R ;i ++){ //Y
        <tr>
    @for(int j = 1; j <= C ;j ++){ //X
            <td bgcolor="@SColor[z]">
          @SeatNAME[z]
            </td>
      //Console.WriteLine(z);
      //Console.WriteLine(SeatNAME[z]);
      z++;
     }
        </tr> } 
    }
</table></div></section>

顯示結果
https://ithelp.ithome.com.tw/upload/images/20200917/20130030ptehOPpTdc.png

DAY11心得:
整個功能還有許多想要修改的部分,我就慢慢做慢慢修,每天都讓隊友收10pm提醒信我真的/images/emoticon/emoticon16.gif
繼續加油吧,快要連心得都不知道要打甚麼的我~


上一篇
零經驗 .NET Core 30 DAY----- Day10 Anchor tag helper 用法
下一篇
零經驗 .NET Core 30 DAY----- Day12 NUnit進行C#單元測試
系列文
零經驗ASP .NET Core 30 DAY全紀錄30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言