如何在 C# .NET CORE 中使用特定条件进行自动映射?

How to automap with a specific condition in C# .NET CORE?

我想在我的特定 DTO 中自动映射填充 workerCategory 键,但我不知道该怎么做。我在构建解决方案时没有遇到任何错误,但是当我进行 API 调用时它会触发错误。

你能帮帮我吗?

public class WorkerRD
    {
        public int Id { get; set; }
        public string Login { get; set; }
        public string Role { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public string AvatarUrl { get; set; }
        public string Iso { get; set; }
        public string Sexe { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public int WorkerCategoryKey { get; set; }
        public string Address { get; set; }
        public string Postcode { get; set; }
        public string Locality { get; set; }

        public IEnumerable<TrWorkerToWorkerCategory> trWorkerToWorkerCategory { get; set; }
    }

CreateMap<TUser, WorkerRD>()
                .ForMember(
                    dest => dest.Firstname,
                    opt => opt.MapFrom(src => src.TWorker.Firstname)
                ).ForMember(
                    dest => dest.Lastname,
                    opt => opt.MapFrom(src => src.TWorker.Lastname)
                ).ForMember(
                    dest => dest.Email,
                    opt => opt.MapFrom(src => src.TWorker.Email)
                ).ForMember(
                    dest => dest.Phone,
                    opt => opt.MapFrom(src => src.TWorker.Phone)
                ).ForMember(
                    dest => dest.Sexe,
                    opt => opt.MapFrom(src => src.TWorker.Sexe)
                ).ForMember(
                    dest => dest.Address,
                    opt => opt.MapFrom(src => src.TWorker.Address)
                ).ForMember(
                    dest => dest.Postcode,
                    opt => opt.MapFrom(src => src.TWorker.Postcode)
                ).ForMember(
                    dest => dest.Locality,
                    opt => opt.MapFrom(src => src.TWorker.Locality)
                ).ForMember(
                    dest => dest.WorkerCategoryKey,
                    opt => opt.MapFrom(src => src.TWorker.TrWorkerToWorkerCategory.Where(tr => tr.IsDefault == true).Select(cat => cat.WorkerCategoryKey))
                ).ForMember(
                    dest => dest.trWorkerToWorkerCategory,
                    opt => opt.MapFrom(src => src.TWorker.TrWorkerToWorkerCategory.Where(tr => tr.IsDefault == true))
                );

我的问题就在这里。我想 select workerCategoryKey(整数)并将其映射到 WorkerCategoryKey 中。 但是没用。

.ForMember(
                    dest => dest.WorkerCategoryKey,
                    opt => opt.MapFrom(src => src.TWorker.TrWorkerToWorkerCategory.Where(tr => tr.IsDefault == true).Select(cat => cat.WorkerCategoryKey))

您的映射不起作用,因为您不能简单地将集合 (.Select(cat => cat.WorkerCategoryKey)) 映射到单个值 (dest.trWorkerToWorkerCategory)。理解这一点的一种简单方法是手动进行类似的映射:

List<int> source = new List<int> { 1, 2, 3 };
int destination = source;

这没有任何意义,但这实际上就是您告诉 AutoMapper 对 WorkerCategoryKey.

当前映射所做的事情

话虽如此,要准确说出您需要做什么并不容易,因为这取决于数据。但无论是什么,您都需要源是单个 int。例如,如果源始终是相同 WorkerCategoryKey 的集合,那么您可以从 .Select(cat => cat.WorkerCategoryKey):

中获取第一项
.ForMember(
    dest => dest.WorkerCategoryKey,
    opt => opt.MapFrom(src => src.TWorker.TrWorkerToWorkerCategory.Where(tr => tr.IsDefault == true).Select(cat => cat.WorkerCategoryKey).First())