本文介绍了如何使用RuleForEach验证收集项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在(成功地)使用以下验证:

I have been using (successfully) the following validation:

RuleFor(x => x.Items)
  .SetCollectionValidator(new ItemValidator())
  .Must(coll => coll.Sum(item => item.Percentage) == 100)
  .When(x => x.Items != null);

由于上述SetCollectionValidator已被弃用,我将其更改为:

As the above SetCollectionValidator is (will be) deprecated, I changed it to:

RuleForEach(x => x.Items)
  .SetValidator(new ItemValidator())
  .Must(coll => coll.Sum(item => item.Percentage) == 100)
  .When(x => x.Items != null);

但是,Sum不再被识别.

我该如何解决?

推荐答案

您可以使用两个单独的规则.其中之一是验证项目,另一项是用于验证集合.

You can use two separate rules. One of them is validate item, and other one is for validation of collection.

RuleForEach(x => x.Items)
  .SetValidator(new ItemValidator());

RuleFor(x => x.Items)
  .Must(coll => coll.Sum(item => item.Percentage) == 100)
  .When(x => x.Items != null);

这篇关于如何使用RuleForEach验证收集项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 15:59