简体   繁体   中英

c# Fill a listView with data from several Linq queries

My code checks several comboboxes and refines the result via several queries. When I check an "Etablissement" in a combobox, a listView displays the list of agents for that Etablishment. This works perfectly. The problem is when I select two or more, I get no result in the list of agents.

Evertime i check one of the list, i call this :

      if (_TousAgents != null && _Agents != null)
            {
                _agents = from agent in _TousAgents //_TousAgents contain a list of all agents indiscriminately
                          where DateFin >= agent.DebutContrat
                            && (agent.FinContrat == null
                            || DateDébut <= agent.FinContrat)
                          select agent;
                if (_SelectedStatut != null && !String.IsNullOrEmpty(_SelectedStatut.IDStatut))
                    _agents = from agent in _agents
                          where agent.Typecontrat == _SelectedStatut.IDStatut
                          select agent;
                if (_SelectedService != null && !String.IsNullOrEmpty(_SelectedService.ID))
                    _agents = from agent in _agents
                              where agent.IDService == _SelectedService.ID
                              select agent;
                if (_SelectedSection != null && !String.IsNullOrEmpty(_SelectedSection.ID))
                    _agents = from agent in _agents
                              where agent.IDSection == _SelectedSection.ID
                              select agent;
                if (_SelectedEmploi != null && !String.IsNullOrEmpty(_SelectedEmploi.IDEmploi))
                    _agents = from agent in _agents
                              where agent.IDEmploi == _SelectedEmploi.IDEmploi
                              select agent;
                if (_SelectedFiliere != null && !String.IsNullOrEmpty(_SelectedFiliere.ID))
                    _agents = from agent in _agents
                              where agent.IDFiliere == _SelectedFiliere.ID
                              select agent;

                foreach (Etablissement etb in EtablissementsUtilisateur)
                    if (etb.IsSelected == true)
                    {
                        _agents = from agent in _agents
                                  where agent.IDEtablissement == etb.IDEtablissement
                                  select agent;
                    } //I think this is the problem


                _Agents.Clear();
                foreach (AgentModel ag in _agents.Distinct().OrderBy(a => a.Prenom).OrderBy(a => a.Nom))
                {
                    _Agents.Add(ag);
                }

                if (_Agents != null && _Agents.Count > 0)
                {
                    if (!String.IsNullOrEmpty(SingleAgentMatricule) && _Agents.SingleOrDefault(ag => ag.Matricule == _SingleAgentMatricule) != null)
                    {
                        _Agents.SingleOrDefault(ag => ag.Matricule == _SingleAgentMatricule).IsSelected = true;
                    }
                    else
                        SelectAllAgents = true;

                }
                RaisePropertyChanged("Agents");

            }

WPF side :

        <ComboBox Name="CmbEtabTout" 
              ItemsSource="{Binding EtablissementsUtilisateur}"
              Grid.IsSharedSizeScope="True"                 
              Grid.Column="2" 
              Grid.ColumnSpan="3" 
              Grid.Row="2" 
              Height="25" 
              Width="250">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="30" />
                        <ColumnDefinition SharedSizeGroup="AgentA" Width="auto" />
                        <ColumnDefinition Width="5" />
                        <ColumnDefinition SharedSizeGroup="AgentB" Width="auto" />
                    </Grid.ColumnDefinitions>
                    <CheckBox IsChecked="{Binding IsSelected}" Command="{Binding DataContext.UpdateListeAgentMulti, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" Grid.Column="0" />
                    <TextBlock Text="{Binding IdEtablissement}" Grid.Column="1"/>
                    <TextBlock Text="{Binding Nom}" Grid.Column="3"/>
                </Grid>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

       <ListView x:Name="LVAgent"
        ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
        ItemsSource="{Binding Agents}" Grid.ColumnSpan="2" Margin="150,0,42,0" Grid.Column="2" Grid.Row="4" Grid.RowSpan="5" >
        <ListView.View>
            <GridView>
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding IsSelected}" 
                                              Command="{Binding DataContext.SelectAgentCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"
                                              CommandParameter="{Binding}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                    <CheckBox IsChecked="{Binding SelectAllAgents}"
                        IsEnabled="True"/>
                </GridViewColumn>
                <GridViewColumn Header="Matricule"
                    Width="110" 
                    DisplayMemberBinding="{Binding Matricule}"/>
                <GridViewColumn Header="Nom" 
                    Width="120"
                    DisplayMemberBinding="{Binding Nom}"/>
                <GridViewColumn Header="Prénom" 
                    Width="120" 
                    DisplayMemberBinding="{Binding Prenom}"/>

            </GridView>
        </ListView.View>
    </ListView>

Exemple of results, with 1 check :

在此处输入图片说明

Everything works.

With 2 or more :

在此处输入图片说明

I'm suppose to fill the ListView with agents from Demo + Demo2018;

I think your linq query is wrong because you are asign it everytime you loop in the foreach. Maybe you need something like this

     _agents = _agents.Where(x=> p = EtablissementsUtilisateur.Where(y=> y.IsSelected == true).Contains(x));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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