如何过滤对象集合 [英] How do I filter a collection of objects

查看:57
本文介绍了如何过滤对象集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有动物课。草食动物类,继承自动物和大象类,继承自食草动物。我还有一个食肉动物类,它继承自动物和老虎类,它继承了食肉动物。我有一个名为动物园的可观察的收藏品,里面有老虎和大象。我有一个与动物园集合挂钩的列表框。例如,当点击一个按钮来显示老虎或显示大象时,我该如何过滤。



这是我将动物园集合绑定到列表框的代码:

 lstBoxAnimals.ItemsSource = zoo; 

解决方案

这是相当的容易做到:假设您的ObservableCollection< Animal>被命名为'ZooAnimals'并且你的ListBox被命名为'TheZoo'..并且有两个Click事件处理程序按钮:

  private   void  TigersOnly_Click( object  sender,EventArgs e)
{
// 过滤ListBox只显示Tigers
TheZoo.DataSource = ZooAnimals.Where(tgr = > tgr Tiger).ToList();

// 替代
// TheZoo.DataSource = ZooAnimals.OfType< Tiger>()。ToList();
}

private void ElephantsOnly_Click( object sender,EventArgs e)
{
// 过滤ListBox只显示大象
TheZoo.DataSource = ZooAnimals.Where(elp = > elp Elephant)。 ToList();

// 替代
// TheZoo.DataSource = ZooAnimals.OfType< Elephant>()。ToList();
}

当然,您可以重置ListBox以显示从动物/ Whatever-Vore派生的类型的所有实例/只需设置:TheZoo.DataSource = ZooAnimals;



欢迎使用CodeProject:)


我不知道我如何过滤对象集合是什么意思。即使您被要求,也没有提供有关您的问题的足够信息。



我建议您阅读:对ObservableCollection进行排序和过滤 [ ^ ] 。上述文章的作者写道, ObservableCollection< T> 允许通过 CollectionViews [ ^ ]。我从来没有用过,但我认为这是个好主意。



我认为以下文章也可能很有趣:

为您的日常自定义集合提供设计时改造 [ ^ ]

收藏(C#和Visual Basic) [ ^ ]



但最强大的工具是Linq [ ^ ]:

如何:从多个源填充对象集合(LINQ) [ ^ ]

使用带有linq的Observable集合 [ ^ ]



试试吧!并带着详细的问题回到这里。

干杯,

Maciej


I have a animal class. a herbivore class which inherits from animal and a elephant class which inherits from herbivore. I also have a carnivore class which inherits from animal and a tiger class which inherits from carnivore. I Have a observable collection called zoo with tigers and elephants. I have a listbox hooked up to the zoo collection. How do I filter for example when a button in clicked just to show tigers or eg to show elephants.

This is the code where I bind the zoo collection to the listbox:

lstBoxAnimals.ItemsSource = zoo;

解决方案

This is quite easy to do: assuming your ObservableCollection<Animal> is named 'ZooAnimals' and your ListBox is named 'TheZoo' .. and there are two buttons with Click EventHandlers:

private void TigersOnly_Click(object sender, EventArgs e)
{
    // filter ListBox to only show Tigers
    TheZoo.DataSource = ZooAnimals.Where(tgr => tgr is Tiger).ToList();

    // an alternative
    // TheZoo.DataSource = ZooAnimals.OfType<Tiger>().ToList();
}

private void ElephantsOnly_Click(object sender, EventArgs e)
{
    // filter ListBox to only show Elephants
    TheZoo.DataSource = ZooAnimals.Where(elp => elp is Elephant).ToList();

    // an alternative
    // TheZoo.DataSource = ZooAnimals.OfType<Elephant>().ToList();
}

Of course, you can reset the ListBox to show all instances of Types derived from Animal/Whatever-Vore/ by simply setting: TheZoo.DataSource = ZooAnimals;

Welcome to CodeProject :)


I have no idea what you mean by "How do i filter a collection of objects". You did not provide enough information about your issue, even if you've been asked for it.

I would suggest to read this: Sorts and filters on ObservableCollection[^]. The author of above article wrote that ObservableCollection<T> allow sorting and filtering operations via CollectionViews[^]. I've never used that, but i think it's quite good idea.

I think below set of articles might be interesting too:
Give Your Everyday Custom Collections a Design-Time Makeover[^]
Collections (C# and Visual Basic)[^]

But the most powerful tool is Linq[^]:
How to: Populate Object Collections from Multiple Sources (LINQ)[^]
Using Observable collection with linq[^]

Try! And come back here with detailed question.
Cheers,
Maciej


这篇关于如何过滤对象集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆