简体   繁体   English

可观察的集合“不同”

[英]Observable collection “distinct by”

I have an observable collection displayed in a listView, and I would like it to display agents that have different emails.我在 listView 中显示了一个可观察的集合,我希望它显示具有不同电子邮件的代理。

If I use.distinct(), all the agents that have, for example, the same first name do not appear.如果我使用.distinct(),则不会出现所有具有相同名字的代理。 I wish everything BUT mail could be the same (two agents can have the same first name, the same company, but they will have two different emails).我希望除了邮件之外的所有东西都可以相同(两个代理可以有相同的名字,相同的公司,但他们会有两封不同的电子邮件)。

I tried this, but I only get the rows that have all distincts fields, except I would only the emails:我试过这个,但我只得到具有所有不同字段的行,除了我只会电子邮件:

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

I looked at this but I don't know how to use it in my case: LINQ's Distinct() on a particular property我看了这个,但我不知道如何在我的情况下使用它: LINQ's Distinct() on a specific property

My listView at the moment:目前我的列表视图:

列表显示

My Database:我的数据库:

数据库

as you can see, some rows do not appear, with different mails.正如你所看到的,有些行没有出现,有不同的邮件。 I would like to display it.我想展示它。

I would like something like that but doesn't work:我想要这样的东西,但不起作用:

     Agents = new ObservableCollection<AgentMailModel>(_TmpAgents.Select(m => m.Mail).Distinct());

Thanks for your help.谢谢你的帮助。

Edit: To be more clear:编辑:为了更清楚:

Person1: Id=1, Name="Test1", Society = "1", Mail= "Mail1@mail.com"
Person2: Id=2, Name="Test1", Society = "1", Mail= "Mail2@mail.com"
Person3: Id=3, Name="Test1", Society = "1", Mail= "Mail2@mail.com"

How can i get Person1 & Person2 only?我怎样才能只获得 Person1 和 Person2?

It looks like you are describing a GroupBy here, the only unclear part is that when you group by an email, at some point (as far as I understood from your question) you need only one record, so you should specify which record to take.看起来您在这里描述的是 GroupBy,唯一不清楚的部分是当您按 email 分组时,在某些时候(据我从您的问题中了解到)您只需要一条记录,因此您应该指定要采用哪条记录. For example in your above grid, you have two people with the 'pcarre@aeimasso.com' email, just decide which one to pick.例如,在您上面的网格中,您有两个名为“pcarre@aeimasso.com”email 的人,只需决定选择哪一个。

For example here am grouping by email, then ordering each group by name and simply picking the first record:例如,这里按 email 分组,然后按名称对每个组进行排序并简单地选择第一条记录:

ObservableCollection<Agent> tmp;
tmp = new ObservableCollection<Agent>(Agents.GroupBy(agent => agent.Email, (key, agent) => agent.OrderBy(e => e.Name).First()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM