C# 在自动映射中使用Discard变量

C# 在自动映射中使用Discard变量,c#,automapper,C#,Automapper,我需要使用自动映射将逗号分隔的字符串变量安全地转换为int-list 在Automapper的6.2.2版中,我是这样做的: cfg.CreateMap<FilterRequest, Filter>() .ForMember(dest => dest.SomeIds, opt => opt.ResolveUsing(src => !string.IsNullOrEmpty(src.SomeIds) ? src.SomeIds.Split(',').Where(x =

我需要使用自动映射将逗号分隔的字符串变量安全地转换为int-list

在Automapper的6.2.2版中,我是这样做的:

cfg.CreateMap<FilterRequest, Filter>()
.ForMember(dest => dest.SomeIds, opt => opt.ResolveUsing(src => !string.IsNullOrEmpty(src.SomeIds) ? src.SomeIds.Split(',').Where(x => int.TryParse(x, out _)).Select(int.Parse).ToList() : null));
TryParse
中的discard变量出错:表达式树可能不包含discard


如何在自动映射中使用discard变量?

您可以使用此重载:

void MapFrom<TResult>(Func<TSource, TDestination, TResult> mappingFunction);

cfg.CreateMap<FilterRequest, Filter>()
    .ForMember(
        dest => dest.SomeIds,
        opt => opt.MapFrom((src, dest) => src.SomeIds
            .Split(',')
            .Where(x => int.TryParse(x, out _))
            .Select(int.Parse)
            .ToList()));
void mappfrom(Func mappingFunction);
cfg.CreateMap()
福门博先生(
dest=>dest.someid,
opt=>opt.MapFrom((src,dest)=>src.SomeIds
.Split(“,”)
.Where(x=>int.TryParse(x,out_u2;))
.Select(int.Parse)
.ToList());

根据错误消息,您可能需要执行类似于
。。。int.TryParse(x,out-var-neverUsed).
您不需要空检查。您也可以从字符串映射,AM将为您转换为int。检查
void MapFrom<TResult>(Func<TSource, TDestination, TResult> mappingFunction);

cfg.CreateMap<FilterRequest, Filter>()
    .ForMember(
        dest => dest.SomeIds,
        opt => opt.MapFrom((src, dest) => src.SomeIds
            .Split(',')
            .Where(x => int.TryParse(x, out _))
            .Select(int.Parse)
            .ToList()));