使用组合框和 LINQ 过滤列表框
Filtering ListBox using combobox and LINQ
我有一个包含 ListBox
和 Combobox
的 winform。在此 ListBox
中首先出现一个客户列表 运行。
我想用 Combobox
过滤 ListBox
中的 "Clients"。
要在 Combobox
中使用字符串 select 填充 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();
}
它有效 "great" 但我的代码不是真正的动态代码而且太长(很多不同的客户端)这就是我想使用 LINQ 的原因。
我阅读了 Microsoft 的文档,但对如何使用它感到很困惑。
有没有人有时间给我指路?
正在添加信息:
我的表格:
我select组合框中我想要的类型:
结果:
谢谢
好的,让我们试一试。如果你想实现过滤,你应该考虑一个合适的结构来表示你的过滤条件。在这种情况下,您的组合框中有一个标签,它绑定到一个唯一的过滤条件。这可以由自定义 class:
表示
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
class 中的 ToString
方法,ListBox
将相应地处理显示。但正如我所见,你已经做到了
我有一个包含 ListBox
和 Combobox
的 winform。在此 ListBox
中首先出现一个客户列表 运行。
我想用 Combobox
过滤 ListBox
中的 "Clients"。
要在 Combobox
中使用字符串 select 填充 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();
}
它有效 "great" 但我的代码不是真正的动态代码而且太长(很多不同的客户端)这就是我想使用 LINQ 的原因。 我阅读了 Microsoft 的文档,但对如何使用它感到很困惑。
有没有人有时间给我指路?
正在添加信息:
我的表格:
我select组合框中我想要的类型:
结果:
谢谢
好的,让我们试一试。如果你想实现过滤,你应该考虑一个合适的结构来表示你的过滤条件。在这种情况下,您的组合框中有一个标签,它绑定到一个唯一的过滤条件。这可以由自定义 class:
表示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
class 中的 ToString
方法,ListBox
将相应地处理显示。但正如我所见,你已经做到了