如何遍历以逗号分隔的字符串属性、拆分字符串并从所有不同的值创建 ICollection

How to iterate through comma-separated string properties, split the strings and create an ICollection from all distinct values

我有一个 class “Movie_Class” 与来自 xml 文件的数据相关。 要读取 xml 文件,我使用 class “StoreDbDataSet”:

public class StoreDbDataSet
{
    public DataTable GetMovies()
    {
        return ReadDataSet().Tables[0];
    }


    internal static DataSet ReadDataSet()
    {
        DataSet ds = new DataSet();
        ds.ReadXml("Peter_Movie_Database.xml");
        return ds;
    }

}

“Movie_Class”:

public class Movie_Class : INotifyPropertyChanged
{

    private string mediaLabel;
    public string MediaLabel
    {
        get { return mediaLabel; }
        set
        {
            mediaLabel = value;
            OnPropertyChanged(new PropertyChangedEventArgs("MediaLabel"));
        }
    }

    private string year;
    public string Year
    {
        get { return year; }
        set
        {
            year = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Year"));
        }
    }

    private string originalTitle;
    public string OriginalTitle
    {
        get { return originalTitle; }
        set
        {
            originalTitle = value;
            OnPropertyChanged(new PropertyChangedEventArgs("OriginalTitle"));
        }
    }

    private string director;
    public string Director
    {
        get { return director; }
        set
        {
            director = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Director"));
        }
    }

    private string category;
    public string Category
    {
        get { return category; }
        set
        {
            category = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Category"));
        }
    }


    public Movie_Class(string originalTitle, string year,
        string director, string mediaLabel, string category)
    {
        OriginalTitle = originalTitle;
        Year = year;
        Director = director;
        MediaLabel = mediaLabel;
        Category = category;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }

}

我创建了一个包含所有不同“年份”属性的集合。请参阅下面代码中的 GetDistinctYear()。然后,我可以将此 Icollection 用作 wpf 中组合框的 ItemsSource。这很好用。 但现在我想对一个名为“类别”的 属性 做同样的事情。问题是这是一个逗号分隔的字符串。 (例如:剧情片、惊悚片、War) 我不知道如何遍历所有“类别”属性、拆分字符串并创建包含所有不同类别的 Icollection。

public class StoreDb
{

    public ICollection<Movie_Class> GetMovies()
    {
        DataSet ds = StoreDbDataSet.ReadDataSet();

        ObservableCollection<Movie_Class> movies = new ObservableCollection<Movie_Class>();
        foreach (DataRow productRow in ds.Tables["Movie"].Rows)
        {
            movies.Add(new Movie_Class((string)productRow["OriginalTitle"],
                (string)productRow["Year"], (string)productRow["Director"],
                (string)productRow["MediaLabel"], (string)productRow["Category"]));
        }
        return movies;
    }

    public ICollection<Movie_Class> GetDistinctYear()
    {

        ICollection<Movie_Class> movies = GetMovies();


        IEnumerable<Movie_Class> matches = from movie in movies
                                           group movie by movie.Year into grps
                                           select grps.First();


        return new ObservableCollection<Movie_Class>(matches.ToList());
    }

    public ICollection<Movie_Class> GetDistinctCategory()
    {


    }


}

我的 .cs 文件中的代码 mainwindow.xaml:

 public partial class MainWindow : Window
{

    private ICollection<Movie_Class> moviesByYear;
    private ICollection<Movie_Class> moviesByCategory;
    private ICollection<Movie_Class> AllMovies;

    public MainWindow()
    {
        InitializeComponent();

        AllMovies = App.StoreDb.GetMovies();

        moviesByYear = App.StoreDb.GetDistinctYear();
        ComboBox_Year.ItemsSource = moviesByYear;

        moviesByCategory = App.StoreDb.GetDistinctCategory();
        ComboBox_Category.ItemsSource = moviesByCategory; 

        ICollectionView view = CollectionViewSource.GetDefaultView(ComboBox_Year.ItemsSource);     
        view.SortDescriptions.Add(new SortDescription("Year", ListSortDirection.Ascending));   
    }

试试这个:

public ICollection<string> GetDistinctCategory()
{
    ICollection<Movie_Class> movies = GetMovies();

    return movies.SelectMany(x => x.Category.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        .Select(x => x.Trim())
        .Distinct()
        .ToList();
}