[英]Filtering a list of objects without linq c#
我有一个称为图像的对象列表,这些对象是通过解析文本文件获得的。 它们包含详细信息,例如类别和说明。 我希望能够在列表中进行搜索以找到具有特定类别的图像,然后在我已设置的表单上显示它们。 我想对它们进行过滤,然后还可以还原到未过滤的视图。
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来解决此问题的最佳方法是什么?
您可以使用List的FindAll
方法:
ImageList = ImageList.FindAll(x => x.Catagory == chosenCatagory);
使用两个列表。 ImageList包含所有数据,而DisplayList仅用您要显示的内容填充。
DisplayList = ImageList.FindAll(x => x.Catagory == chosenCatagory);
如果要还原,请将DisplayList设置回ImageList。
DisplayList = ImageList;
如果您想做更多高级的事情,也可以看看CollectionView 。 它支持过滤,分组和排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.