iT邦幫忙

0

C# Asp.NET Core API使用AutoMapper問題

  • 分享至 

  • xImage

我正在學習AutoMapper,可是我不知道來源的類型這邊應該怎麼處理。

這是我準備要使用至AutoMapper的資料,使用LINQ準備了一下資料;
我有一個叫ToDoList的資料表,有兩個欄位,分別是「InsertEmployeeId」「UpdateEmployeeId」,我需要分別Join到Employeey資料表中,並返回各自的Name。

準備完資料後我嘗試使用AutoMapper套件,我希望他返回一個可列舉的TodoListSelectDto類型的資料。

var Item = from todoList in _dbContext.TodoList
           join insertEmployeeList in _dbContext.Employee on todoList.InsertEmployeeId equals insertEmployeeList.EmployeeId into tempinsertEmployeeList
           from insertEmployeeList in tempinsertEmployeeList.DefaultIfEmpty()
           join updateEmployeeList in _dbContext.Employee on todoList.UpdateEmployeeId equals updateEmployeeList.EmployeeId into tempupdateEmployeeList
           from updateEmployeeList in tempupdateEmployeeList.DefaultIfEmpty()
           select new { todoList , insertName = insertEmployeeList.Name, updateName = updateEmployeeList.Name};
           
var map = _mapper.Map<IEnumerable<TodoListSelectDto>>(Item);

這邊就是我的問題所在了,他必須指定一種類型,我的XXX該輸入什麼他才會會順利地接受我Item的資料

public class TodoListProfile : Profile
{
    public TodoListProfile()
    {
        CreateMap<XXX, TodoListSelectDto>();
    }
}

ToList的欄位
{ TodoId, Name, InsertTime, UpdateTime, Enable, Orders, InsertEmployeeId, UpdateEmployeeId, InsertEmployee, UpdateEmployee}

Employee的欄位
{EmployeeId, Name}

Dto
TodoListSelectDto
{ TodoId, Name, InsertTime, UpdateTime, Enable, Orders, InsertEmployeeName, UpdateEmployeeName}

以上感謝各位協助,如有不清楚的地方再麻煩糾正,再次感謝提供建議

看更多先前的討論...收起先前的討論...
mobone iT邦新手 5 級 ‧ 2024-04-23 17:03:27 檢舉
你的ITEM回傳回來的資料內容是甚麼?
你指的是第一段程式碼嗎?
如果是:
(區域變數)IQueryable<'a>? Item
'a是 new { TodoList todoList, string insertName, string Name}
mobone iT邦新手 5 級 ‧ 2024-04-23 19:00:55 檢舉
我有改了程式碼 你在試試看
恩,不對... 我不是要新增一個類別,我主要是再練DTO,可是要一個一個指派很麻煩,看到網路上使用AutoMapper,可以更加方便,因此遇到了困難
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
mobone
iT邦新手 5 級 ‧ 2024-04-23 17:10:00
public partial class Item
{
    public string Name {get;set;}
}

public class TodoListProfile : Profile
{
    public TodoListProfile()
    {
        CreateMap<Item, TodoListSelectDto>();
    }
}
//我不知道你XXX內容是甚麼東西
//{ TodoId, Name, InsertTime, UpdateTime, Enable, Orders, InsertEmployeeId, UpdateEmployeeId, InsertEmployee, UpdateEmployee}

//如果你的TodoList是上面這樣,那你就

public class Item
{
    public int TodoId { get; set; }
    public string Name { get; set; }
    public DateTime InsertTime { get; set; }
    public DateTime UpdateTime { get; set; }
    public bool Enable { get; set; }
    public int Orders { get; set; }
    public int InsertEmployeeId { get; set; }
    public int UpdateEmployeeId { get; set; }
    public string InsertEmployee { get; set; }
    public string UpdateEmployee { get; set; }
}

public class TodoListProfile : Profile
{
    public TodoListProfile()
    {
        CreateMap<Item, TodoListSelectDto>();
    }
}

0
MemoNote
iT邦見習生 ‧ 2024-04-23 17:44:03

我試著不用Profile的方式去建立mapping 因爲我也不知道Class是什麽東西
就寫了一個方法讓他在程式中建立對應

// 使用 LINQ 進行篩選
var filteredSources = from s in sourceList select new {s.Id,s.Name };

IEnumerable<Destination> way1_DestinationData = new List<Destination>();
IEnumerable<Destination> way1_ResultData = setmapper(filteredSources, way1_DestinationData);
    /// <summary>
    /// 建立AutoMapper
    /// </summary>
    /// <typeparam name="Ts"></typeparam>
    /// <typeparam name="Td"></typeparam>
    /// <param name="source"></param>
    /// <param name="_">參數沒用到類型需要</param>
    /// <returns></returns>
    public static IEnumerable<Td> setmapper<Ts, Td>(IEnumerable<Ts> source, IEnumerable<Td> _)
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Ts, Td>();
        });

        // 創建映射器
        IMapper mapper = config.CreateMapper();

        // 將源序列映射到目標序列
        IEnumerable<Td> destination = mapper.Map<IEnumerable<Ts>, IEnumerable<Td>>(source);

        return destination;
    }
   

我要發表回答

立即登入回答