尝试处理菜单和电影

时间:2018-09-11 14:50:53

标签: c#

我有2个C#问题:

  • 问题1:如何获得20部评分最高的电影?
  • 问题2:如何在其中制作菜单?

我的代码:

using System;
using System.Collections.Generic
using System.Xml;
using System.Linq;

namespace XXSlutuppgift_Movie { 
    class Program {
        static void Main(string[] args) {
            List<Movie> movieCollection = GetMovieCollection();
            List<Movie> Orderedlist = movieCollection.OrderBy(Film => Film.name).ToList();
            List<Movie> Top20MovieRating = movieCollection.OrderBy(Film => Film.rating).ToList();
            List<Movie> MovieYear = movieCollection.OrderBy(Film => Film.year).ToList();
            List<Movie> LetterSearch = movieCollection.OrderBy(Film => Film.name).ToList();

            static List<Movie> GetMovieCollection() {
                List<Movie> list = new List<Movie>();
                XmlDocument doc = new XmlDocument();
                doc.Load(System.Environment.CurrentDirectory + "/moviecollection.xml");
                XmlNode node = doc.DocumentElement.SelectSingleNode("/MovieCollection/Movies");
                foreach(XmlNode row in node.ChildNodes) {
                    Movie item = new Movie();
                    item.id = Int32.Parse(row.SelectSingleNode("Id").InnerText);
                    item.name = row.SelectSingleNode("Name").InnerText;
                    item.rating = Double.Parse(row.SelectSingleNode("Rating").InnerText.Replace(".", ","));
                    item.votes = Int32.Parse(row.SelectSingleNode("Votes").InnerText);
                    item.year = Int32.Parse(row.SelectSingleNode("Year").InnerText);
                    list.Add(item);
                }
            return list
        }
    }
}

2 个答案:

答案 0 :(得分:1)

首先,您需要知道如何从Help Center发布问题,关于问题的全貌,如果您有一个列表,并且想要从高到低返回数据,您将得到它,如下面的代码所示:

        var list = new List<int>();
        list.Add(1);
        list.Add(2);
        list.Add(3);
        list.Add(4);
        list.Add(5);
        list.Add(6);

        var desNumber=list.OrderByDescending(x => x);

在这种情况下,如果您有一个名为Movie的对象,其中包含Ratting属性,并且想要获得前20名电影,则可以使用take方法,该方法指定列表中拍摄对象的数量:

        var movieList = new GetMovieList();
        var topRatedMovies = movieList.OrderByDescending(m => m.Ratting).Take(20);

GetMovieList()是一种返回电影列表的方法。

答案 1 :(得分:0)

我修复了您的代码:

using System; 
using System.Collections.Generic; 
using System.Xml; 
using System.Linq; 
namespace XXSlutuppgift_Movie 
{ 
    class Program 
    {
        static void Main(string[] args)
        {
            List<Movie> movieCollection = GetMovieCollection();
            List<Movie> Orderedlist = movieCollection.OrderBy(Film => Film.name).ToList();
            List<Movie> Top20MovieRating = movieCollection.OrderBy(Film => Film.rating).ToList();
            List<Movie> MovieYear = movieCollection.OrderBy(Film => Film.year).ToList();
            List<Movie> LetterSearch = movieCollection.OrderBy(Film => Film.name).ToList();
        }

        static List<Movie> GetMovieCollection() 
        { 
            List<Movie> list = new List<Movie>(); 
            XmlDocument doc = new XmlDocument(); 
            doc.Load(System.Environment.CurrentDirectory + "/moviecollection.xml"); 
            XmlNode node = doc.DocumentElement.SelectSingleNode("/MovieCollection/Movies"); 
            foreach(XmlNode row in node.ChildNodes) 
            { 
                Movie item = new Movie(); 
                item.id = Int32.Parse(row.SelectSingleNode("Id").InnerText); 
                item.name = row.SelectSingleNode("Name").InnerText; 
                item.rating = Double.Parse(row.SelectSingleNode("Rating").InnerText.Replace(".", ",")); 
                item.votes = Int32.Parse(row.SelectSingleNode("Votes").InnerText); 
                item.year = Int32.Parse(row.SelectSingleNode("Year").InnerText); 
                list.Add(item); 
            } return list; 
        } 
    }
    public class Movie
    {
        public int id { get;set;}
        public string name { get;set;}
        public double rating { get;set;}
        public int votes { get;set;}
        public int year { get;set;}

    }
}