我正在學習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}
以上感謝各位協助,如有不清楚的地方再麻煩糾正,再次感謝提供建議
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>();
}
}
我試著不用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;
}