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