Reputation: 55
I have this validation in mvc
public static ValidationResult validaUsuariosNaLista(Reuniao item)
{
var requeridos = item.Requeridos.Select(x => x.Login).Any();
var informados = item.Informados.Select(y => y.Login).Any();
var opcionais = item.Opcionais.Select(z => z.Login ? z.Login : null).Any();
if (requeridos == informados || requeridos == opcionais || informados == opcionais)
return new ValidationResult(Resources.Validations.ValidaUsuarioMesmaLista);
return ValidationResult.Success;
}
I try make a different if in line
var opcionais = item.Opcionais.Select(z => z.Login ? z.Login : null).Any();
but show error
Error 3 Cannot implicitly convert type 'string' to 'bool'
z.Login is a string
validation is to make that the field has no value it receives null.
Without that it bursts the error that is null.
I want him to receive null without giving error for it.
It selects the z.login on the list if the same login is in the other lists he'm the error.
How can I do this "if" that way?
Upvotes: 0
Views: 2915
Reputation: 415600
z.Login is a string
validation is to make that the field has no value it receives null.
Do it like this:
Edit: Updated this section to also look for duplicates
public static ValidationResult validaUsuariosNaLista(Reuniao item)
{
var requeridos = item.Requeridos.Any(x => string.IsNullOrWhiteSpace(x.Login));
var informados = item.Informados.Any(x => string.IsNullOrWhiteSpace(x.Login));
var opcionais = item.Opcionais .Any(x => string.IsNullOrWhiteSpace(x.Login));
//does every item have a value?
if (requeridos || informados || opcionais)
return new ValidationResult(Resources.Validations.ValidaUsuarioMesmaLista);
//are all of the items unique?
if (item.Requeridos.Count() + item.Informados.Count() + item.Opcionais.Count() >
item.Requeridos.Concat(item.Informados).Concat(item.Opcionais).Distinct().Count)
{
//some items are duplicated
}
return ValidationResult.Success;
}
And, for fun, to avoid repeating code:
public static ValidationResult validaUsuariosNaLista(Reuniao item)
{
var HasEmptyValue = s => s.Any(x => string.IsNullOrWhiteSpace(x.Login));
//does every item have a value?
if (HasEmptyValue(item.Requeridos) || HasEmptyValue(item.Informados) || HasEmptyValue(item.Opcionais))
return new ValidationResult(Resources.Validations.ValidaUsuarioMesmaLista);
//are all of the items unique?
if (item.Requeridos.Count() + item.Informados.Count() + item.Opcionais.Count() >
item.Requeridos.Concat(item.Informados).Concat(item.Opcionais).Distinct().Count)
{
//some items are duplicated
}
return ValidationResult.Success;
}
Though I'm not 100% on how well type inference will work here... I'd have to see what the compiler makes of it, and I don't have your types to test with. But at worst, you'd just have to be explicit on the var
declaration.
Upvotes: 0
Reputation: 218798
If z.Login
is a string, then this expression is invalid:
z.Login ? z.Login : null
The first element in that expression needs to be a bool
. What exactly are you trying to examine in that condition? Whether or not z.Login
is null
? If that's the case then you don't need a condition at all:
.Select(z => z.Login)
Since you'd just be replacing null
with, well, null
. Or if you want to interpret empty strings as null
then you can use something like this:
.Select(z => string.IsNullOrEmpty(z.Login) ? nulll : z.Login)
Though it's not really clear what you're trying to accomplish with this line of code in the first place. .Any()
is going to return a bool
indicating whether or not any matching element exists in the collection at all. But since you're not using a predicate in .Any()
, it's going to return true
if there is any element in the collection at all. Which not only renders the .Select()
pointless, but also doesn't tell you anything about that condition in the .Select()
.
Perhaps you are trying to find if there are any null
values in the collection?:
item.Opcionais.Any(z => z.Login == null)
or any "null or empty" values?:
item.Opcionais.Any(z => string.IsNullOrEmpty(z.Login))
or the opposite, any non-empty values?:
item.Opcionais.Any(z => !string.IsNullOrEmpty(z.Login))
and so on...
Upvotes: 1
Reputation: 2601
I think what you're trying to do is check if Login is null or empty. Assuming you want opcionais
to be a boolean based on your .Any()
statement:
var opcionais = item.Opcionais.Select(z => z.Login ? z.Login : null).Any();
should be
var opcionais = item.Opcionais.Any(z => !string.IsNullOrEmpty(z.Login));
Upvotes: 0