Beispiel #1
0
        static void Main(string[] args)
        {
            List <Passenger> paxList = Passenger.LoadPassengers();

            List <Passenger> survivor = paxList.FindAll(p => p.Survivor);
            HashSet <string> classes  = new HashSet <string>();
            HashSet <string> roles    = new HashSet <string>();

            //  classes and roles are Sets so this will code will produce a unique set of these attributes
            //  classes = paxList.Select(p => p.PaxClass).ToHashSet();
            foreach (var pax in paxList)
            {
                classes.Add(pax.PaxClass);
                roles.Add(pax.Role);
            }
            //  same but using Linq code
            //  classes = paxList.Select(p => p.PaxClass).ToHashSet();
            //  roles = paxList.Select(p => p.Role).ToHashSet();

            //  display the unique roles of passengers
            foreach (var role in roles)
            {
                Console.WriteLine(role);
            }
            //  same but using some Linq code
            //  roles.ToList().ForEach(r => Console.WriteLine(r));

            List <NBAPlayer> players = NBAPlayer.LoadRecords();
            List <NBAPlayer> phxSuns = players.FindAll(p => p.Team.Equals("PHX"));

            foreach (var player in phxSuns)
            {
                Console.WriteLine(player);
            }
            //  same but using Linq
            //  phxSuns.ForEach(p => Console.WriteLine(p));

            /*
             *                 LINQ and Collection methods
             *
             * List<Passenger> passengers = Passenger.LoadPassengers();
             * var survivors = passengers.FindAll(p => p.Survivor);
             * https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.add?view=netcore-3.1
             *  Add         AddRange    Clear       Contains    ConvertAll  Exists
             *  Find        FindAll     FindIndex   FindLast    FindLastIndex
             *  ForEach     GetRange    IndexOf     Insert      InsertRange
             *  LastIndexOf Remove      RemoveAll   RemoveAt    RemoveRange
             *  Reverse     Sort        ToArray     TrimExcess  TrueForAll
             *
             *  https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.any?view=netcore-3.1
             *  Any         All         Average     Contains    Count
             *  Distinct    ElementAt   Empty       Except      First
             *  GroupBy     Interesct   Join        Last        Max Min
             *  OrderBy(Desc)           Prepend     Range       Repeat
             *  Reverse     Select      SelectMany  Single      Skip
             *  SkipLast    SkipWhile   Sum         Take        TakeLast
             *  TakeWhile   ThenBy      Union       Where       Zip
             *  ToArray     ToDictionary  ToHashSet ToList      ToLookUp
             */
        }
        /**
         *      LoadRecords
         *          Reads a csv file
         *          parses the lines on comma
         *          creates an NBAPlayer with the fields
         *          adds the player to the List
         *          returns list to the calling application
         *
         */
        public static List <NBAPlayer> LoadRecords()
        {
            String fileName = @"C:\Projects\csv\Players.csv";   //  the CSV file with all players

            List <NBAPlayer> players = new List <NBAPlayer>();  //  list of all NBA players
            StreamReader     textIn  = null;                    //  this is file read
            string           line    = "";

            try
            {
                textIn = new StreamReader(fileName);            //  open the file for reading
                line   = textIn.ReadLine();                     //  read and throw away the header line
                while (line != null)                            //  read each line from the file
                {
                    string[] prop = line.Split(",");            //  split the data fields apart

                    //  create the NBA Player
                    NBAPlayer player = new NBAPlayer(prop[1], prop[2], decimal.Parse(prop[3]), prop[4],
                                                     float.Parse(prop[5]), float.Parse(prop[6]), float.Parse(prop[7]), float.Parse(prop[8]),
                                                     float.Parse(prop[9]), float.Parse(prop[10]), float.Parse(prop[11]), float.Parse(prop[12]),
                                                     float.Parse(prop[13]), float.Parse(prop[14]), float.Parse(prop[15]), float.Parse(prop[16]),
                                                     float.Parse(prop[17]), float.Parse(prop[18]), float.Parse(prop[19]), float.Parse(prop[20]),
                                                     float.Parse(prop[21]));
                    players.Add(player);                        //  add player to the list of players
                    line = textIn.ReadLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error Reading file: " + fileName);
                Console.WriteLine("\t" + line);
                Console.WriteLine(ex.ToString());
                return(null);
            }
            finally
            {
                if (textIn != null)
                {
                    textIn.Close();
                }
            }
            return(players);
        }