簡體   English   中英

如何遍歷逗號分隔的字符串屬性,拆分字符串並從所有不同的值創建ICollection

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

我有一個與來自xml文件的數據相關的“ Movie_Class”類。 要讀取xml文件,我使用“ 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;
    }

}

“電影類”:

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

}

我創建了所有不同的“年份”屬性的Icollection。 請參見下面的代碼中的GetDistinctYear()。 然后,我可以將此Icollection用作wpf中組合框的ItemsSource。 這很好。 但是現在我想對稱為“類別”的屬性執行相同的操作。 問題是這是一個逗號分隔的字符串。 (例如:戲劇,驚悚片,戰爭)我不知道如何遍歷所有“類別”屬性,拆分字符串並創建包含所有不同類別的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()
    {


    }


}

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

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

警告:未經測試

對於每個逗號分隔的字符串category ,請進行拆分:

var cats = new List<string>();

foreach(var csvCat in csvCats){
    var cat_list = csvCat.split(","); 
    cats.AddRange(cat_list);
}

然后,只需使用Linq即可獲得不同的代碼:

var distinctCats = cats.Distinct();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM