Example #1
0
        private static void CheckCampersCabin()
        {
            using (CampSleepawayContext context = new CampSleepawayContext())
            {
                //======
                // Ask for Next of kin info
                string bigfirstname = MenuUtils.AskForString("Vad heter målsmannen i förnamn?");
                string biglastname  = MenuUtils.AskForString("Vad heter målsmannen i efternamn?");
                // Query data from next of kin

                NextOfKin nextOfKin = (from nextOfKins in context.NextOfKins
                                       where nextOfKins.FirstName == bigfirstname &&
                                       nextOfKins.LastName == biglastname
                                       select nextOfKins).FirstOrDefault();
                context.Entry(nextOfKin).Collection(c => c.Campers).Load();

                int      campersCount     = nextOfKin.Campers.Count();
                string[] campersFirstName = new string[campersCount];
                string[] campersLastName  = new string[campersCount];
                int[]    camperIds        = new int[campersCount];
                int      i = 0;
                foreach (var campersInQuery in nextOfKin.Campers)
                {
                    campersFirstName[i] = campersInQuery.FirstName;
                    campersLastName[i]  = campersInQuery.LastName;
                    camperIds[i]        = campersInQuery.CamperId;
                    i++;
                }
                // Checks out and prints all the campers of the next of kin.
                int choice = MenuUtils.AlternetivesMenu(0, campersFirstName, "Vilken unge vill du hämta?");

                //var camperQuery = (from camper in context.Campers
                //                   where camper.LastName == campersLastName[choice] &&
                //                   camper.FirstName == campersFirstName[choice]
                //                   select camper.Cabin).ToList();
                string camperFirstName = campersFirstName[choice];
                string camperLastName  = campersLastName[choice];
                int    camperId        = camperIds[choice];
                //======


                string camperName  = MenuUtils.AskForString("Vad heter parveln i förnamn?");
                var    camperQuery = (from camper in context.Campers
                                      where camper.CamperId == camperId
                                      select camper.Cabin).ToList();


                // visa info om valda kabin
                Cabin cabinInfo = camperQuery[0];
                context.Entry(cabinInfo).Reference(c => c.Counselor).Load();
                context.Entry(cabinInfo).Collection(c => c.Campers).Load();
                if (cabinInfo != null)
                {
                    Console.WriteLine($"\nLägerhusets namn: {cabinInfo.Name}");

                    if (cabinInfo.Counselor != null)
                    {
                        Console.WriteLine($"\nGruppledarens namn: {cabinInfo.Counselor.FirstName}");
                    }
                    if (cabinInfo.Campers != null)
                    {
                        PrintCamperInfo(context, cabinInfo);
                    }
                }
                else
                {
                    Console.WriteLine("Hittade inget lägerhus till den kamparen");
                }
            }
            MenuUtils.PauseUntilFeedback("Tryck en knapp för att fortsätta");
        }