【问题标题】:c# Fill a listView with data from several Linq queriesc# 用来自多个 Linq 查询的数据填充 listView
【发布时间】:2023-03-15 21:30:02
【问题描述】:

我的代码检查了几个组合框并通过几个查询优化了结果。 当我在组合框中选中“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;

【问题讨论】:

  • 在代码中放置一个断点并检查您是否进入了多个 If 语句。代码看起来像是进入每个 IF 语句,然后只返回最后一个 IF。
  • 没关系,我得到每个“if”语句
  • 您是否应该将每个响应添加到 _agent 而不是覆盖旧数据?如果两个 IF 返回相同的 _agent 会发生什么情况?
  • 最后我在将它们添加到列表之前使用了 distinct。并且每个 if = 一个组合框,我对代理进行排序
  • 是的,只要您添加到 _agent 就可以使用,而不是您现在所做的只是以一组选择结束。

标签: c# wpf linq mvvm


【解决方案1】:

我认为您的 linq 查询是错误的,因为您每次在 foreach 中循环时都会分配它。 也许你需要这样的东西

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

【讨论】:

  • EtablissementUtilisateur 是一个可观察的集合,不包含“包含”定义,但我会尝试以这种方式检查!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
  • 1970-01-01
  • 1970-01-01
  • 2017-05-24
相关资源
最近更新 更多