如何通过使用其他属性过滤可观察集合来检索类的属性?

时间:2017-01-18 14:53:38

标签: c# class uwp

我正在开发VS2015社区的UWP应用程序,我已经制作了一个候选人类和另一个候选人类CandidateManager。在CandidateManager类中,我创建了一个方法getCandidateByCategory,以按类别编号和序列号对候选进行排序。我在ScienceAndITClub.Xaml.cs页面上调用了该方法,但现在我在检索过滤项目的name属性时遇到问题(将其设置为RBcand1 radiobutton的内容),&#下面的红线是# 39;'它显示错误"运营商'。'不能应用于' void'"类型的操作数。此外,总是只有一个具有特定类别编号和序列号的候选人。

这是Science& ITClub.Xaml.cs:

public sealed partial class ScienceAndITClub : Page
{
    private ObservableCollection<Candidate> Candidates;

    public ScienceAndITClub()
    {
        this.InitializeComponent();
        CandidateManager.GetAllCandidates(Candidates);


        var v = new Candidate();
        v = (CandidateManager.GetCandidatesByCategory(Candidates, CandidateCategoryNo.I, CandidateSNo.I).ToList<Candidate>);
        string cand1name = v.Name;
        RBcand1.Content = cand1name;
    }

    private void NextButton_Click(object sender, RoutedEventArgs e)
    {

    }
}

这里是候选类的代码,即Candidate.cs:

public class Candidate
{
    public int VoteCount { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
    public CandidateSNo S_no { get; set; }
    public CandidateCategoryNo Category_no { get; set; }

}

public enum CandidateCategoryNo
{
    I,
    II,
    III,
    IV,
    V,
    VI
}

public enum CandidateSNo
{
    I,
    II,
    III
}

这里是CandidateManager类的代码,即CandidateManager.cs:

 class CandidateManager
{
    public static void GetAllCandidates(ObservableCollection<Candidate>   candidates)
    {
        var allCandidates = getCandidate();
        candidates.Clear();
        allCandidates.ForEach(p => candidates.Add(p));
    }
     public static void GetCandidatesByCategory(ObservableCollection<Candidate> candidates, CandidateCategoryNo candidateCategory, CandidateSNo Sno)
    {
        var allCandidates = getCandidate();
        var filteredCandidates = allCandidates.Where(p => p.Category_no == candidateCategory).ToList();
        filteredCandidates = allCandidates.Where(p => p.S_no == Sno).ToList();
        candidates.Clear();
        filteredCandidates.ForEach(p => candidates.Add(p));
    }
    private static List<Candidate> getCandidate()
    {
        var _candidate = new List<Candidate>();
        _candidate.Add(new Candidate() { S_no = CandidateSNo.I, Category_no = CandidateCategoryNo.I, VoteCount = 0, Category = "Science and IT Club", Name = "A" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.II, Category_no = CandidateCategoryNo.I, VoteCount = 0, Category = "Science and IT Club", Name = "B" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.III, Category_no = CandidateCategoryNo.I, VoteCount = 0, Category = "Science and IT Club", Name = "C" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.I, Category_no = CandidateCategoryNo.II, VoteCount = 0, Category = "Dance Club", Name = "D" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.II, Category_no = CandidateCategoryNo.II, VoteCount = 0, Category = "Dance Club", Name = "E" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.III, Category_no = CandidateCategoryNo.II, VoteCount = 0, Category = "Dance Club", Name = "F" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.I, Category_no = CandidateCategoryNo.III, VoteCount = 0, Category = "Music Club", Name = "G" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.II, Category_no = CandidateCategoryNo.III, VoteCount = 0, Category = "Music Club", Name = "H" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.III, Category_no = CandidateCategoryNo.III, VoteCount = 0, Category = "Music Club", Name = "I" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.I, Category_no = CandidateCategoryNo.IV, VoteCount = 0, Category = "Social Service Club", Name = "J" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.II, Category_no = CandidateCategoryNo.IV, VoteCount = 0, Category = "Social Service Club", Name = "K" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.III, Category_no = CandidateCategoryNo.IV, VoteCount = 0, Category = "Social Service Club", Name = "L" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.I, Category_no = CandidateCategoryNo.V, VoteCount = 0, Category = "Sports Club", Name = "M" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.II, Category_no = CandidateCategoryNo.V, VoteCount = 0, Category = "Sports Club", Name = "N" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.III, Category_no = CandidateCategoryNo.V, VoteCount = 0, Category = "Sports Club", Name = "O" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.I, Category_no = CandidateCategoryNo.VI, VoteCount = 0, Category = "School Captain", Name = "P" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.II, Category_no = CandidateCategoryNo.VI, VoteCount = 0, Category = "School Captain", Name = "Q" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.III, Category_no = CandidateCategoryNo.VI, VoteCount = 0, Category = "School Captain", Name = "R" });
        return _candidate;
    }
}

此外,在这个节目中,我将采取候选人&#39;作为用户输入的名称(我还没有将其设计为函数,所以我把所有名称都只是字母表),所以请你告诉我如何将文本框中的输入转换为候选人的name属性。

希望你能帮忙...谢谢,祝你有愉快的一天! :)

1 个答案:

答案 0 :(得分:0)

您正在传递集合对象并在“过滤”中修改它。所以,在你的情况下,你可以调用方法,这将改变ScienceAndITClub页面的集合 - 所以从中读取。如果你使用.FirstOrDefault(),它将为你提供第一个条目(你只期望一个),如果集合中没有任何内容,则为null。

这应该解决您的错误问题。但是,一旦开始获取数据,您将看到更多问题。

public sealed partial class MainPage : Page
{
    private ObservableCollection<Candidate> Candidates = new ObservableCollection<Candidate>();
    public MainPage()
    {
        this.InitializeComponent();


        CandidateManager.GetAllCandidates(Candidates);

        //this line alters the Candidates collection
        CandidateManager.GetCandidatesByCategory(Candidates, CandidateCategoryNo.I, CandidateSNo.I);
        //now that the method is done, you can use the Candidates collection to get the results
        var v = Candidates.FirstOrDefault();    //will get first candidate, or null if collection is empty
        string cand1name = v.Name;
        RBcand1.Content = cand1name;
    }
}

<强>更新

我有一点时间吃午饭,所以我把你可以考虑的东西放在一起,当你开发你的项目时。让CandidateManager管理候选者并成为数据的来源。在真实世界的应用程序中,我会去MVVM路由并创建存储库和服务层......但这应该让你开始。如果您的应用程序需要,请更改为ObservableCollection。

public class CandidateManager
{
    //first time CandidateManager is used, it will populate backing store
    static CandidateManager()
    {
        //initialize data collection
        Candidates = new List<Candidate>();
        GetCandidateData().ForEach(p => Candidates.Add(p));
    }
    /// <summary>
    /// Backing Store
    /// </summary>
    private static List<Candidate> Candidates { get; set; }

    /// <summary>
    /// Returns all candidates
    /// </summary>
    /// <returns></returns>
    public static List<Candidate> GetAllCandidates()
    {
        return Candidates;      
    }

    /// <summary>
    /// Returns all candidates for a specified category
    /// </summary>
    /// <param name="candidateCategory"></param>
    /// <returns></returns>
    public static List<Candidate> GetCandidatesByCategory(CandidateCategoryNo candidateCategory)
    {
        return Candidates.Where(c => c.Category_no == candidateCategory).ToList();
    }

    /// <summary>
    /// Returns specific candidate based on unique category/sno combination.
    /// Returns null if no match
    /// </summary>
    /// <param name="candidateCategory"></param>
    /// <param name="Sno"></param>
    /// <returns></returns>
    public static Candidate GetCandidateByCategoryAndSno(CandidateCategoryNo candidateCategory, CandidateSNo Sno)
    {
        return Candidates.Where(c => c.S_no == Sno && c.Category_no == candidateCategory).FirstOrDefault();
    }

    /// <summary>
    /// data creation
    /// </summary>
    /// <returns></returns>
    private static List<Candidate> GetCandidateData()
    {
        var _candidate = new List<Candidate>();
        _candidate.Add(new Candidate() { S_no = CandidateSNo.I, Category_no = CandidateCategoryNo.I, VoteCount = 0, Category = "Science and IT Club", Name = "A" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.II, Category_no = CandidateCategoryNo.I, VoteCount = 0, Category = "Science and IT Club", Name = "B" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.III, Category_no = CandidateCategoryNo.I, VoteCount = 0, Category = "Science and IT Club", Name = "C" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.I, Category_no = CandidateCategoryNo.II, VoteCount = 0, Category = "Dance Club", Name = "D" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.II, Category_no = CandidateCategoryNo.II, VoteCount = 0, Category = "Dance Club", Name = "E" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.III, Category_no = CandidateCategoryNo.II, VoteCount = 0, Category = "Dance Club", Name = "F" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.I, Category_no = CandidateCategoryNo.III, VoteCount = 0, Category = "Music Club", Name = "G" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.II, Category_no = CandidateCategoryNo.III, VoteCount = 0, Category = "Music Club", Name = "H" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.III, Category_no = CandidateCategoryNo.III, VoteCount = 0, Category = "Music Club", Name = "I" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.I, Category_no = CandidateCategoryNo.IV, VoteCount = 0, Category = "Social Service Club", Name = "J" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.II, Category_no = CandidateCategoryNo.IV, VoteCount = 0, Category = "Social Service Club", Name = "K" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.III, Category_no = CandidateCategoryNo.IV, VoteCount = 0, Category = "Social Service Club", Name = "L" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.I, Category_no = CandidateCategoryNo.V, VoteCount = 0, Category = "Sports Club", Name = "M" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.II, Category_no = CandidateCategoryNo.V, VoteCount = 0, Category = "Sports Club", Name = "N" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.III, Category_no = CandidateCategoryNo.V, VoteCount = 0, Category = "Sports Club", Name = "O" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.I, Category_no = CandidateCategoryNo.VI, VoteCount = 0, Category = "School Captain", Name = "P" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.II, Category_no = CandidateCategoryNo.VI, VoteCount = 0, Category = "School Captain", Name = "Q" });
        _candidate.Add(new Candidate() { S_no = CandidateSNo.III, Category_no = CandidateCategoryNo.VI, VoteCount = 0, Category = "School Captain", Name = "R" });
        return _candidate;
    }
}

然后,在您的UI代码中,只需使用管理器来检索数据。

public MainPage()
{
    this.InitializeComponent();

    var c = CandidateManager.GetCandidateByCategoryAndSno(CandidateCategoryNo.II, CandidateSNo.II);
    RBcand1.Content = c?.Name;
}