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

时间:2018-08-04 12:33:11

标签: c# linq

我有一个称为图像的对象列表,这些对象是通过解析文本文件获得的。它们包含详细信息,例如类别和说明。我希望能够在列表中进行搜索以找到具有特定类别的图像,然后在我已设置的表单上显示它们。我想对它们进行过滤,然后还可以还原到未过滤的视图。

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; }
}

这就是我要在Linq中做的事

string chosenCatagory = CatagoryComboBox.Text;

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

不使用Linq来解决此问题的最佳方法是什么?

2 个答案:

答案 0 :(得分:2)

您可以使用List的FindAll方法:

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

答案 1 :(得分:0)

使用两个列表。包含所有数据的ImageList和仅用您要显示的内容填充的DisplayList。

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

如果您想将DisplayList还原回ImageList。

DisplayList = ImageList;

如果您想做更高级的事情,也请看看CollectionView。它支持过滤,分组和排序。