我一直在使用(成功)以下验证:
RuleFor(x => x.Items)
.SetCollectionValidator(new ItemValidator())
.Must(coll => coll.Sum(item => item.Percentage) == 100)
.When(x => x.Items != null);
由于上面的SetCollectionValidator(将被弃用),我将其更改为:
RuleForEach(x => x.Items)
.SetValidator(new ItemValidator())
.Must(coll => coll.Sum(item => item.Percentage) == 100)
.When(x => x.Items != null);
但是,Sum不再被识别.
我怎样才能解决这个问题?
最佳答案 您可以使用两个单独的规则.其中一个是验证项目,另一个是验证集合.
RuleForEach(x => x.Items)
.SetValidator(new ItemValidator());
RuleFor(x => x.Items)
.Must(coll => coll.Sum(item => item.Percentage) == 100)
.When(x => x.Items != null);