コード例 #1
0
        public static List <TimePerson> GetPersons(int startyear, int endYear)
        {
            // create a list of Time persons (instantiate a new list)
            List <TimePerson> list = new List <TimePerson>();

            // get the path of your timeperson.csv file
            // getting the path is not as simple as ../../../ have to use
            // the Path or Environment class
            var path = @"../Lab11-TimePerson/wwwroot/personOfTheYear.csv";

            // once you get the file path,
            // read all the lines and save it into an array of strings
            string[] allNames = File.ReadAllLines(path);

            // traverse through the strings for each line item
            // remember CSV is delimited by commas.

            string[] Header = allNames[0].Split(',');

            for (int i = 1; i < allNames.Length; i++)
            {
                string[] splitName = allNames[i].Split(',');

                TimePerson person = new TimePerson(
                    splitName[0] == "" ? 0 : Convert.ToInt32(splitName[0]),
                    splitName[1],
                    splitName[2],
                    splitName[3],
                    splitName[4] == "" ? 0 : Convert.ToInt32(splitName[4]),
                    splitName[5] == "" ? 0 : Convert.ToInt32(splitName[5]),
                    splitName[6],
                    splitName[7],
                    splitName[8]
                    );
                list.Add(person);
            }

            // use LINQ to filter out with the years that you brought in against your list of persons

            List <TimePerson> query2 = list.Where(x => x.Year >= startyear && x.Year <= endYear).ToList();


            //return your list of persons
            return(query2);
        }
コード例 #2
0
        /// <summary>
        /// Method that returns a list of people after query
        /// </summary>
        /// <param name="startYear">int</param>
        /// <param name="endYear">int</param>
        /// <returns>list of people</returns>
        public static List <TimePerson> GetPersons(int startYear, int endYear)
        {
            //pathing to wwwroot/csv file
            //string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../wwwroot/personOfTheYear.csv");
            string path    = Environment.CurrentDirectory;
            string newPath = Path.GetFullPath(Path.Combine(path, @"wwwroot/personOfTheYear.csv"));

            //list instantiation with all the properties from csv.file
            List <TimePerson> personList = new List <TimePerson>();

            //skipping one to avoid titles, and no interference with string to int
            var lines = File.ReadAllLines(newPath)
                        .Skip(1);

            //traverse through the strings for each line item
            foreach (string line in lines)
            {
                //split by commas to retrieve word for specific heading
                string[] rowColumn = line.Split(",");

                //instantiating new timeperson with values from csv.file
                TimePerson timePerson = new TimePerson
                {
                    Year      = Convert.ToInt32(rowColumn[0]),
                    Honor     = rowColumn[1] != "" ? rowColumn[1] : "N/A",
                    Name      = rowColumn[2] != "" ? rowColumn[2] : "N/A",
                    Country   = rowColumn[3] != "" ? rowColumn[3] : "N/A",
                    BirthYear = rowColumn[4] != "" ? rowColumn[4] : "N/A",
                    DeathYear = rowColumn[5] != "" ? rowColumn[5] : "N/A",
                    Title     = rowColumn[6] != "" ? rowColumn[6] : "N/A",
                    Category  = rowColumn[7] != "" ? rowColumn[7] : "N/A",
                    Context   = rowColumn[8] != "" ? rowColumn[8] : "N/A"
                };
                //add all the new instantiated values to list
                personList.Add(timePerson);
            }

            //utilizing LINQ to filter out according to user input on start year and end year
            var query = personList
                        .Where(p => p.Year >= startYear && p.Year <= endYear)
                        .OrderBy(p => p.Year).ToList();

            //return result list
            return(query);
        }
コード例 #3
0
        /// <summary>
        /// Instantiation of TimePerson, getting the timeperson.csv file, receiving all data as a string type, converting int types back to int, then running query
        /// </summary>
        /// <param name="beginningYear">int beginningYear as inputted by end user</param>
        /// <param name="endYear">int endYear as inputted by end user</param>
        /// <returns></returns>
        public static List <TimePerson> GetPerson(int beginningYear, int endYear)
        {
            //instantiate a new list of Time persons
            List <TimePerson> timePeople = new List <TimePerson>();

            //get the path of your timeperson.csv file - google how to get the root file path for an mvc web application
            var path    = Environment.CurrentDirectory;
            var newPath = Path.GetFullPath(Path.Combine(path, @"wwwroot/personOfTheYear.csv"));

            //Console.WriteLine(timeData);
            string[] timeData = File.ReadAllLines(newPath);

            //once you get the file path, read all the lines and save it into an array of strings
            //(new string[] { "Year", "Honor", "Name", "Country", "BirthYear", "DeathYear", "Title", "Category", "Context" });

            for (var i = 1; i < timeData.Length; i++)
            {
                string[] timeDataSplit = timeData[i].Split(',');

                //travserse through the strings for each line item
                //remember CSV is delimited by commas.
                TimePerson timePerson = new TimePerson
                                        (
                    timeDataSplit[0] == "" ? 0 : Convert.ToInt32(timeDataSplit[0]), //ternary to convert value to int and return 0 if empty
                    timeDataSplit[1],
                    timeDataSplit[2],
                    timeDataSplit[3],
                    timeDataSplit[4] == "" ? 0 : Convert.ToInt32(timeDataSplit[4]), //ternary to convert value to int and return 0 if empty
                    timeDataSplit[5] == "" ? 0 : Convert.ToInt32(timeDataSplit[5]), //ternary to convert value to int and return 0 if empty
                    timeDataSplit[6],
                    timeDataSplit[7],
                    timeDataSplit[8]
                                        );

                timePeople.Add(timePerson);
            }

            //use LINQ to filter out with the years you brought in against your list of persons

            List <TimePerson> query = timePeople.Where(x => x.Year >= beginningYear && x.Year <= endYear).ToList();

            return(query);
        }