【问题标题】:Filtering ListBox using combobox and LINQ使用组合框和 LINQ 过滤列表框
【发布时间】:2019-05-17 12:46:46
【问题描述】:

我有一个包含ListBoxCombobox 的winform。在此ListBox 中,首次运行时会出现一个客户列表。

我想用Combobox 过滤ListBox 中的“客户”。

使用Combobox 中选择的字符串填充ListBox 我正在使用:

private void FillListBox()
{
    this.lstClient.Items.Clear();

    foreach (Client c in this.client)
    {
        if (this.cBox.Text == "All")
            this.lstClient.Items.Add(c.ToString());
        else
            if (this.cBox.Text == "Retail" && c.GetType() == typeof(RetailClient))
                this.lstClient.Items.Add(c.ToString());
    }

    this.lstClient.Sorted = true;
}

之后我从 ComboBox 的事件中调用此方法:

private void cBox_TextChanged(object sender, EventArgs e)
{
    this.FillListBox();
}

它工作“很棒”,但我的代码不是真正动态的而且太长(很多不同的客户端),这就是我想使用 LINQ 的原因。 我阅读了 microsoft 的文档,但我对如何使用它感到很困惑。

有人有时间给我指路吗?

添加信息:

我的表格:

我在 ComboBox 中选择我想要的类型:

结果:

谢谢

【问题讨论】:

  • 我不确定我是否了解您如何过滤您的列表。你能举个图形例子吗?
  • 什么是this.client?为什么需要使用GetType()来确定type?是偶然的List<object> 吗?您可以使用简单的 LINQ 查询设置ListBox.DataSource 过滤源列表。
  • 是的 this.client 是一个 List ,其中 client 是一个类。我使用“类型”是因为在我的 List 中有多种客户(零售、批发、...)
  • 你能发布你的Client类吗?

标签: c# linq combobox listbox


【解决方案1】:

好的,让我们试一试。如果你想实现过滤,你应该考虑一个合适的结构来表示你的过滤标准。在这种情况下,您的组合框中有一个标签,该标签绑定到唯一的过滤条件。这可以用一个自定义类来表示:

public class SortingRepresentation
{
    public string DisplayLabel { get; set; }
    public Type ClientType { get; set; }        
}

现在您可以创建这些条件的列表并将其推入组合框:

List<SortingRepresentation> sortingFields = new List<SortingRepresentation>();

public Form1()
{
    sortingFields.Add(new SortingRepresentation{ DisplayLabel = "All", TypeCriterion = typeof(Client) });
    sortingFields.Add(new SortingRepresentation{ DisplayLabel = "Only Retail", TypeCriterion = typeof(Client_A) });
    sortingFields.Add(new SortingRepresentation{ DisplayLabel = "Only Wholesale", TypeCriterion = typeof(Client_B) });
    sortingFields.Add(new SortingRepresentation{ DisplayLabel = "Only Human Wholesale", TypeCriterion = typeof(Client_C) });

    cBox.DisplayMember = "DisplayLabel";
    cBox.DataSource = sortingFields;
}

当组合框中的选择发生变化时,您现在可以捕获所选项目(类型为 SortingRepresentation 并将其作为过滤器传递给 FillListBox

private void cBox_SelectedIndexChanged(object sender, EventArgs e)
{
    FillListBox((SortingRepresentation)cBox.SelectedItem);
}

现在您可以使用此对象内的Type TypeCriterion 来过滤您的列表:

private void FillListBox(SortingRepresentation sortcriterion)
{
    this.lstClient.DataSource = null;

    this.lstClient.DataSource = client
            .Where(x => x.GetType() == sortcriterion.TypeCriterion || // either you are of this type
                        x.GetType().BaseType == sortcriterion.TypeCriterion // or your parent is of this type
                   ).ToList();
}

由于您使用的是列表框,您可以将排序列表直接绑定到DataSource 并完成它。为了正确显示,您需要覆盖 Client 类中的 ToString 方法,ListBox 将相应地处理显示。但我看到你已经做到了

【讨论】:

  • 感谢您的解决方案。它不是基于 LINQ,但似乎比我的要好。
  • @Nan0 该解决方案完全基于 linq。看看过滤器。对 So 表示感谢的最佳方式是将答案标记为已接受。快乐编码
  • 我不知道这很抱歉。所以我问了一个基于 Linq 的解决方案,再次感谢!
猜你喜欢
  • 2019-05-09
  • 2011-01-18
  • 2018-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
相关资源
最近更新 更多