4-7.ASP.NET Core Web API 入門教學 - 同時取得父子資料

ASP.NET Core Web API 入門教學

影片講解(有字幕)


這篇簡單來提一下如何同時把父子資料給撈出來

以我這邊的例子,每一個代辦事項(TodoList),可能都會有自己的上傳檔案(UploadFile),那在這邊要如何將關聯的兩者同時撈出來呢?

首先一樣要先建立UploadFile的Dto

namespace Todo.Dtos
{
    public class UploadFileDto
    {
        public Guid UploadFileId { get; set; }
        public string Name { get; set; }
        public string Src { get; set; }
        public Guid TodoId { get; set; }
    }
}

如果有做外鍵關聯的話,很簡單就如下,將他Include進來後即可

var result = _todoContext.TodoLists
    .Include(a => a.InsertEmployee)
    .Include(a => a.UpdateEmployee)
    .Include(a => a.UploadFiles)
    .Select(a => a);

最後我們就做一個Dto的資料轉換

return result.ToList().Select(a => ItemToDto(a));

整個程式會長這樣

[HttpGet]
public IEnumerable<TodoListDto> Get()
{
    var result = _todoContext.TodoLists
        .Include(a => a.InsertEmployee)
        .Include(a => a.UpdateEmployee)
        .Include(a => a.UploadFiles)
        .Select(a => a);

    return result.ToList().Select(a => ItemToDto(a));
}

那DTO轉換的函式程式碼如下

private static TodoListDto ItemToDto(TodoList a)
{
    return new TodoListDto
    {
        Enable = a.Enable,
        InsertEmployeeName = a.InsertEmployee.Name,
        InsertTime = a.InsertTime,
        Name = a.Name,
        Orders = a.Orders,
        TodoId = a.TodoId,
        UpdateEmployeeName = a.UpdateEmployee.Name,
        UpdateTime = a.UpdateTime,
        UploadFiles = a.UploadFiles.Select(a => new UploadFileDto
        {
            Name = a.Name,
            Src = a.Src,
            TodoId = a.TodoId,
            UploadFileId = a.UploadFileId
        }).ToList()
    };
}

影片中是用foreach的方式處理UploadFileDto的轉換,這邊則是簡單用LINQ去完成。

執行結果如下圖,就會看到父資料下有關連撈出子資料UploadFile

那如果今天沒有做外鍵關聯該如何去撈取呢?

這邊我們改示範撈取一筆的程式碼片段

[HttpGet("{id}")]
public TodoListDto Get(Guid id)
{
    var result = (from a in _todoContext.TodoLists
                  where a.TodoId == id
                  select new TodoListDto
                  {
                      Enable = a.Enable,
                      InsertEmployeeName = a.InsertEmployee.Name,
                      InsertTime = a.InsertTime,
                      Name = a.Name,
                      Orders = a.Orders,
                      TodoId = a.TodoId,
                      UpdateEmployeeName = a.UpdateEmployee.Name,
                      UpdateTime = a.UpdateTime,
                      UploadFiles = (from b in _todoContext.UploadFiles
                                     where a.TodoId == b.TodoId
                                     select new UploadFileDto
                                     {
                                         Name = b.Name,
                                         Src = b.Src,
                                         TodoId = b.TodoId,
                                         UploadFileId = b.UploadFileId
                                     }).ToList()
                  }).SingleOrDefault();
    return result;
}

直接用LINQ就可以一起把父子資料都給撈出來了。

以上就是簡單示範,感覺寫文章越來越沒靈感了,想看詳細的流程可以看影片

範例檔下載:下載

新手分享學習成果,若有錯誤,煩請告知修正,感謝🙏




Copyright © 凱哥寫程式 2022 | Powered by TalllKai ❤