簡體   English   中英

c# 使用來自多個 Linq 查詢的數據填充 listView

[英]c# Fill a listView with data from several Linq queries

我的代碼檢查多個組合框並通過多個查詢優化結果。 當我在組合框中選中“Etablissement”時,listView 會顯示該 Etablishment 的代理列表。 這完美地工作。 問題是當我選擇兩個或更多時,我在代理列表中沒有得到任何結果。

每當我檢查列表中的一個時,我稱之為:

      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方面:

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

結果示例,帶有 1 個檢查:

在此處輸入圖片說明

一切正常。

有 2 個或更多:

在此處輸入圖片說明

我想用來自 Demo + Demo2018 的代理填充 ListView;

我認為你的 linq 查詢是錯誤的,因為你每次在 foreach 中循環時都會給它賦值。 也許你需要這樣的東西

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM