简体   繁体   English

不使用linq过滤对象列表C#

[英]Filtering a list of objects without linq c#

I have a list of objects called images which are obtained from parsing through a text file. 我有一个称为图像的对象列表,这些对象是通过解析文本文件获得的。 They contain details such as the catagory and description. 它们包含详细信息,例如类别和说明。 I want to be able to search through the list to find the images with a certain catagory and then display them on a form i have setup. 我希望能够在列表中进行搜索以找到具有特定类别的图像,然后在我已设置的表单上显示它们。 I want to filter through them and then also be able to revert back to the unfiltered view aswell. 我想对它们进行过滤,然后还可以还原到未过滤的视图。

class Image
{
    public string FileName { set; get; }
    public string Description { set; get; }
    public string Catagory { set; get; }
    public string Date { set; get; }
    public string Comments { set; get; }
}

This is what i want to do in Linq 这就是我要在Linq做的

string chosenCatagory = CatagoryComboBox.Text;

ImageList = ImageList.Where(x => x.Catagory == chosenCatagory).ToList();

What would be the best way to approach this without using Linq? 不使用Linq来解决此问题的最佳方法是什么?

您可以使用List的FindAll方法:

ImageList = ImageList.FindAll(x => x.Catagory == chosenCatagory);

Use two Lists. 使用两个列表。 ImageList which contains all data and DisplayList which is filled only with the stuff you want to display. ImageList包含所有数据,而DisplayList仅用您要显示的内容填充。

DisplayList = ImageList.FindAll(x => x.Catagory == chosenCatagory);

And if you want to revert set DisplayList back to ImageList. 如果要还原,请将DisplayList设置回ImageList。

DisplayList = ImageList;

Also have a look at CollectionView when you wanna do more advanced stuff. 如果您想做更多高级的事情,也可以看看CollectionView It supports filtering, grouping and sorting. 它支持过滤,分组和排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM