0

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?

4
  • .Select(z => z.Login) ? Should do what I think you want, but then it isn't entirely clear what you want. Commented Nov 13, 2015 at 16:39
  • 4
    z.Login is string and you are trying to use it as a conditional part in conditiona(ternary) operator z.Login ? z.Login : null ??? Commented Nov 13, 2015 at 16:40
  • 4
    The error is telling you exactly what the issue is.. Commented Nov 13, 2015 at 16:40
  • use the debugger.. hover over the word Anyand what it returns wonder why some many Developers these days neglect to use the debugger.. Commented Nov 13, 2015 at 16:48

3 Answers 3

1

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...

Sign up to request clarification or add additional context in comments.

3 Comments

Alright, but "if" if (requeridos == informados || requeridos == opcionais || informados == opcionais) show error Operator '==' cannot be applied to operands of type 'bool' and 'System.Collections.Generic.IEnumerable<string>'
the main reason for this is validation check for the same item on more than one list
@OAprendiz: What on Earth are you even trying to determine in that condition? Maybe it would start to make sense to me if I understood those variable names, but I don't. It really seems like you're just typing random code without any actual logic.
0

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.

4 Comments

Alright, but "if" if (requeridos == informados || requeridos == opcionais || informados == opcionais) show error Operator '==' cannot be applied to operands of type 'bool' and 'System.Collections.Generic.IEnumerable<string>'
Notice that I changed your if code, too, because your old condition is also wrong. Use it the way I wrote it.
the main reason for this is validation check for the same item on more than one list
That's completely different from what your question is asking about. Until now you've just wanted to make sure they had a value. You'll need to do a LOT more work to compare the items in the list against each other (perhaps a hashset?).
0

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)); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.