Sephystos
Sephystos

Reputation: 69

how can I use "distinct" with all my property

I have a method that allows me to create an agent list. This list of agents contains "Nom", "Prenom", "Matricule", "Mail".

I use.distinct() to sort them, but this only takes into account "Nom", "Prenom", "Matricule". This does not check if the emails are different. How do I proceed?

The creation of my agent list:

private ObservableCollection<AgentMailModel> _Agents;
public ObservableCollection<AgentMailModel> Agents
{
    get
    {
        return _Agents;
    }
    set
    {
        if (value != _Agents)
        {
            _Agents = value;
            RaisePropertyChanged("Agents");
        }
    }
}

foreach (Destinataire dst in (await _dataService.GetDestinatairesAsync()))
        _TmpAgents.Add(new AgentMailModel() { Matricule = dst.Matricule, Nom = dst.Nom, Prenom = dst.Prenom, Mail = dst.Mail });

foreach (AgentModel ag in (await _dataService.GetAgentsContratsAsync()))
        _TmpAgents.Add(new AgentMailModel() { Matricule = ag.Matricule, Nom = ag.Nom, Prenom = ag.Prenom, Mail = ag.Mail });

Agents = new ObservableCollection<AgentMailModel(_TmpAgents.Distinct());

My list in WPF :

list

My database :

db

As you can see :
It displays "carré" (cause "Nom" is different, It also works with a different "Prenom" or "Matricule) and only one "carre" (without "é").

Distinct() doesn't work with my Mails. Any tips ?

Upvotes: 0

Views: 69

Answers (1)

Ygalbel
Ygalbel

Reputation: 5519

You have two possibilities.

First one is to create a class of IEqualityComparer and implement a full comparison. you can find an example here.

The second one is to convert the objects to JSON, and made of comparison of strings instead of objects.

Upvotes: 2

Related Questions