Is there a way to get rid of the .FirstOrDefault()
with the following setup. I love using the yield statement but I want to condense the IsRequired method to the point where I dont have to use .FirstOrDefault()
.
PlayerValidator
protected override IEnumerable<ValidationResult> Validate(PlayerModel entity, IValidationProvider validationProvider)
{
yield return ValidationResultHelper.IsRequired(entity.Profile.FirstName, "First Name").FirstOrDefault();
if (string.IsNullOrWhiteSpace(entity.Profile.LastName))
yield return new Required("Last Name");
}
ValidatorHelper
public IEnumerable<ValidationResult> IsRequired(string text, string name)
{
if (string.IsNullOrWhiteSpace(text))
yield return new Required(name);
}
foreach
would be another option (and it wouldn't return erroneouslynull
in the case that there is no validation error as in your example):