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
string chosenCatagory = CatagoryComboBox.Text;
ImageList = ImageList.Where(x => x.Catagory == chosenCatagory).ToList();
What would be the best way to approach this without using 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.
DisplayList = ImageList.FindAll(x => x.Catagory == chosenCatagory);
And if you want to revert set DisplayList back to ImageList.
DisplayList = ImageList;
Also have a look at CollectionView when you wanna do more advanced stuff. It supports filtering, grouping and sorting.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.