且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自动映射器的条件被忽略

更新时间:2022-05-17 08:54:58

我认为您需要分两个步骤进行ConditionMapFrom:

I think you need to do the Condition and MapFrom in two steps:

.ForMember(c => c.Prop1, o => o.Condition(c => !c.IsSourceValueNull));
.ForMember(c => c.Prop1, o => o.MapFrom(f => f.Prop1.Aggregate(...));

如果条件评估为false,则将永远不会使用MapFrom.

The MapFrom will never be used if the Condition evaluates to false.

编辑

嗯...这似乎不起作用.我以为我以前在某处使用过它.您可以只使用MapFrom:

Hmmm... That doesn't seem to work. I thought I had used that before somewhere. You could resort to just the MapFrom:

.MapFrom(f => f.Prop1 == null ? null : f.Prop1.Aggregate(...));