Reputation: 55
I have three collections of the same model, only changing their names.
want to check the three lists so that:
1) If an element in either list is already in another burst an error
2) When I try to validate the Any does not accept null value.
How do they accept that the string is null?
but I can not.
where I am going wrong?
item.Requeridos
> ICollection<MeetingUser>
item.Informados
> ICollection<MeetingUser>
item.Opcionais
> ICollection<MeetingUser>
public static ValidationResult validaUsuariosNaLista(Reuniao item)
{
var requeridos = item.Requeridos.Select(x => string.IsNullOrWhiteSpace(x.Login)).Any();
var informados = item.Informados.Select(x => string.IsNullOrWhiteSpace(x.Login)).Any();
var opcionais = item.Opcionais .Select(x => string.IsNullOrWhiteSpace(x.Login)).Any();
if (item.Requeridos.Count() + item.Informados.Count() + item.Opcionais.Count() > item.Requeridos.Concat(item.Informados).Concat(item.Opcionais).Distinct().Count())
{
return new ValidationResult(Resources.Validations.sameUserInOtherList);
}
return ValidationResult.Success;
}
Upvotes: 1
Views: 69
Reputation: 55
Final answer:
if the object can also be null use item.Opcionais
as example
Tks @Christos so much.
Yours answers were very good
public class ReuniaoValidation
{
public static ValidationResult validaUsuariosNaLista(Reuniao item)
{
var requeridos = item.Requeridos.Select(x => string.IsNullOrWhiteSpace(x.Login)).Any();
var informados = item.Informados.Select(x => string.IsNullOrWhiteSpace(x.Login)).Any();
var opcionais = item.Opcionais ?? Enumerable.Empty<MeetingUser>();
var listasCombinadas = (item.Requeridos.Concat(item.Informados)).Concat(item.Opcionais ?? Enumerable.Empty<MeetingUser>());
if (listasCombinadas.GroupBy(x => x.Login).Any(gr => gr.Count() > 1))
{
item.AddOdm = false;
return new ValidationResult(Resources.Validations.ValidaUsuarioMesmaLista);
}
return ValidationResult.Success;
}
Upvotes: 0
Reputation: 53958
where I am going wrong?
In the way that Distinct
works. According to MSDN
The Distinct(IEnumerable) method returns an unordered sequence that contains no duplicate values. It uses the default equality comparer, Default, to compare values.
The default equality comparer, Default, is used to compare values of the types that implement the IEquatable generic interface. To compare a custom data type, you must implement this interface and provide your own GetHashCode and Equals methods for the type.
In your case you have a custom data type, MeetingUser
What can you do?
If you avoid the use of Distinct
, you can try something like the below:
// Initially you concat the three list to one list
var combinedLists = (item.Requeridos.Concat(item.Informados)).Concat(item.Opcionais);
// Then you group them by the Login. If there is any group with more that 1
// element then you have the same login more that one time.
var result = combinedLists.GroupBy(x=>x.Login)
.Any(gr=>gr.Count()>1);
Otherwise, if you stick with the Distinct
, you have to implement the IEquatable
interface.
Upvotes: 3