Exemplo n.º 1
0
        public DogOwnerListViewModel GetAllDogOwners()
        {
            var dogOwners             = _DogOwnerService.GetAllDogOwners();
            var dogs                  = _DogService.GetAllDogs();
            var dogOwnerListViewModel = new DogOwnerListViewModel
            {
                DogOwnerViewModels = dogOwners.GroupJoin(dogs,
                                                         owner => owner.OwnerId,
                                                         dog => dog.OwnerId,
                                                         (owner, dogCollection) => new DogOwnerViewModel()
                {
                    OwnerName = owner.OwnerName, DogNames = dogCollection.Select(x => x.DogName).ToList()
                }
                                                         ).ToList()
            };

            return(dogOwnerListViewModel);
        }
Exemplo n.º 2
0
        public void TestDogService()
        {
            var mockDogRepository = new Mock <DogRepository>();

            mockDogRepository.Setup(x => x.GetAllDogs()).Returns(
                new List <Dog>()
            {
                new Dog()
                {
                    OwnerId = 1,
                    DogName = "TestDog"
                }
            }
                );

            var dogService = new DogService(mockDogRepository.Object);
            var dogs       = dogService.GetAllDogs();

            Assert.IsNotNull(dogs);
            Assert.AreEqual(1, dogs.Count);
            Assert.AreEqual(1, dogs.Single().OwnerId);
            Assert.AreEqual("TestDog", dogs.Single().DogName);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var people = PersonService.GetAllPeople();

            var dogs = DogService.GetAllDogs();

            Person a = people.FirstOrDefault(x => x.FirstName == "Cristofer");

            if (a != null)
            {
                a.Dogs = new List <Dog>();
                Dog jack  = dogs.FirstOrDefault(x => x.Name == "Jack");
                Dog ellie = dogs.FirstOrDefault(x => x.Name == "Ellie");
                Dog tilly = dogs.FirstOrDefault(x => x.Name == "Tilly");
                Dog hank  = dogs.FirstOrDefault(x => x.Name == "Hank");
                a.Dogs.Add(jack);
                a.Dogs.Add(ellie);
                a.Dogs.Add(tilly);
                // a.Dogs.Add(hank);
            }
            //foreach (var dog in a.Dogs)
            //{
            //    Console.WriteLine(dog.Name);
            //}


            Person b = people.FirstOrDefault(x => x.FirstName == "Freddy");

            if (b != null)
            {
                b.Dogs = new List <Dog>();
                Dog oscar  = dogs.FirstOrDefault(x => x.Name == "Oscar");
                Dog toby   = dogs.FirstOrDefault(x => x.Name == "Toby");
                Dog chanel = dogs.FirstOrDefault(x => x.Name == "Chanel");
                Dog bo     = dogs.FirstOrDefault(x => x.Name == "Bo");
                Dog scout  = dogs.FirstOrDefault(x => x.Name == "Scout");

                b.Dogs.Add(oscar);
                b.Dogs.Add(toby);
                b.Dogs.Add(chanel);
                b.Dogs.Add(bo);
                b.Dogs.Add(scout);
                //b.PrintAllDogs();
            }

            Person c = people.FirstOrDefault(x => x.FirstName == "Erin");

            if (c != null)
            {
                c.Dogs = new List <Dog>();
                Dog trixie = dogs.FirstOrDefault(x => x.Name == "Trixie");
                Dog archie = dogs.FirstOrDefault(x => x.Name == "Archie");
                Dog max    = dogs.FirstOrDefault(x => x.Name == "Max");
                c.Dogs.Add(trixie);
                c.Dogs.Add(archie);
                c.Dogs.Add(max);
                // c.PrintAllDogs();
            }
            Person ameliaFromList = people.FirstOrDefault(x => x.FirstName == "Amelia");

            if (ameliaFromList != null)
            {
                ameliaFromList.Dogs = new List <Dog>();
                Dog abby   = dogs.FirstOrDefault(x => x.Name == "Abby");
                Dog shadow = dogs.FirstOrDefault(x => x.Name == "Shadow");
                ameliaFromList.Dogs.Add(abby);
                ameliaFromList.Dogs.Add(shadow);
                //ameliaFromList.PrintAllDogs();
            }

            Person larryFromList = people.FirstOrDefault(x => x.FirstName == "Larry");

            if (larryFromList != null)
            {
                larryFromList.Dogs = new List <Dog>();
                Dog zoe   = dogs.FirstOrDefault(x => x.Name == "Zoe");
                Dog ollie = dogs.FirstOrDefault(x => x.Name == "Ollie");
                larryFromList.Dogs.Add(zoe);
                larryFromList.Dogs.Add(ollie);
                //larryFromList.PrintAllDogs();
            }

            Person ericaFromList = people.FirstOrDefault(x => x.FirstName == "Erika");

            if (ericaFromList != null)
            {
                ericaFromList.Dogs = new List <Dog>();
                ericaFromList.Dogs.AddRange(dogs.Where(x => x.Race == Race.Retriever));
                //ericaFromList.PrintAllDogs();
            }
            if (c != null)
            {
                c.Dogs.AddRange(dogs.Where(x => x.Name == "Chet"));
                c.Dogs.AddRange(dogs.Where(x => x.Name == "Ava"));
            }
            Person august = people.FirstOrDefault(x => x.FirstName == "August");

            if (august != null)
            {
                august.Dogs = new List <Dog>();
                august.Dogs.AddRange(dogs.Where(x => x.Name == "Diesel"));
                august.Dogs.AddRange(dogs.Where(x => x.Name == "Rigby"));
            }

            #region Exercises

            var excercise1 = people
                             .Where(x => x.FirstName.ToLower().StartsWith("r"))
                             .OrderByDescending(x => x.Age)
                             .ToList();

            //foreach (var perosns in excercise1)
            //{
            //    Console.WriteLine(perosns.FirstName);
            //}

            var browndogs = dogs
                            .Where(x => x.Color == Color.Brown)
                            .Where(x => x.Age >= 3)
                            .OrderBy(x => x.Age)
                            .ToList();
            foreach (var browndog in browndogs)
            {
                //Console.WriteLine(browndog.Name + " " + browndog.Age);
            }

            var personsWithDogs = people
                                  .Where(x => x.Dogs != null && x.Dogs.Count() > 2)
                                  .OrderByDescending(x => x.FirstName)
                                  .ToList();
            foreach (var person1 in personsWithDogs)
            {
                // Console.WriteLine(person1.FirstName);
            }
            var personsWithOneDog = people
                                    .Where(x => x.Dogs != null)
                                    .Where(x => x.Dogs.All(y => y.Race == x.Dogs[0].Race))
                                    .ToList();

            var personsWithOneDogBetter = people
                                          .Where(x => x.Dogs != null)
                                          .Where(x => x.Dogs.Select(f => f.Race).Distinct().Count() == 1)
                                          .ToList();

            foreach (var person1 in personsWithOneDog)
            {
                //Console.WriteLine(person1.FirstName);
            }

            var Freddysdogs = people
                              .FirstOrDefault(x => x.FirstName.Equals("Freddy"));

            var result5 = Freddysdogs.Dogs.Where(x => x.Age > 1).GroupBy(x => x.Race).ToList();
            foreach (var groupOfDogs in result5)
            {
                Console.WriteLine($"Race:{groupOfDogs.Key}");

                foreach (var dog in groupOfDogs)
                {
                    Console.WriteLine(dog.Name);
                }
            }

            //var result6 = people.Reverse());
            //var count = people.Count;
            //var result6=people.Skip(count - 10).GroupBy(x => x.Age).ToList();
            // var last10People = people.OrderByDescending(x => x.Age).Take(10).ToList();
            var grouper = people.OrderByDescending(x => x.Age).GroupBy(x => x.Age).Take(10).ToList();


            foreach (var groupOfDogs in grouper)
            {
                Console.WriteLine($"Age:{groupOfDogs.Key}");

                foreach (var person in groupOfDogs)
                {
                    Console.WriteLine(person.FirstName);
                }
            }

            // 6. Find and print last 10 persons grouped by their age.


            // 7. Find and print all dogs names from Cristofer, Freddy, Erin and Amelia, grouped by color and ordered by name - ASCENDING ORDER.
            // 8. Find and persons that have same dogs races and order them by name length ASCENDING, then by age DESCENDING.
            // 9. Find the last dog of Amelia and print all dogs form other persons older than Amelia, ordered by dogs age DESCENDING.
            // 10. Find all developers older than 20 with more than 1 dog that contains letter 'e' in the name and print their names and job positions.

            #endregion
        }
Exemplo n.º 4
0
        //[Route("Index")]
        //[Route("Dog/Index")]
        public IActionResult Index()
        {
            var dogs = service.GetAllDogs();

            return(View(dogs));
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            var people = PersonService.GetAllPeople();

            var dogs = DogService.GetAllDogs();

            #region Exercises

            //==============================
            // TODO LINQ expressions :)
            // Your turn guys...
            //==============================

            //PART 1
            // 1. Take person Cristofer and add Jack, Ellie, Hank and Tilly as his dogs.
            // 2. Take person Freddy and add Oscar, Toby, Chanel, Bo and Scout as his dogs.
            // 3. Add Trixie, Archie and Max as dogs from Erin.
            // 4. Give Abby and Shadow to Amelia.
            // 5. Take person Larry and Zoe, Ollie as his dogs.
            // 6. Add all retrievers to Erika.
            // 7. Erin has plus Chet and Ava
            // 8. and now give Diesel to August thah previously has just Rigby

            //PART 2 - LINQ
            // 1. Find and print all persons firstnames starting with 'R', ordered by age - DESCENDING ORDER.
            // 2. Find and print all brown dogs names and ages older than 3 years, ordered by age - ASCENDING ORDER.
            // 3. Find and print all persons with more than 2 dogs, ordered by name - DESCENDING ORDER.
            // 4. Find and print all persons names, last names and job positions that have just one race type dogs.
            // 5. Find and print all Freddy`s dogs names older than 1 year, grouped by dogs race.
            // 6. Find and print last 10 persons grouped by their age.
            // 7. Find and print all dogs names from Cristofer, Freddy, Erin and Amelia, grouped by color and ordered by name - ASCENDING ORDER.
            // 8. Find and persons that have at least one same dogs race and order them by name length ASCENDING, then by age DESCENDING.
            // 9. Find the last dog of Amelia and print all dogs form other persons older than Amelia, ordered by dogs age DESCENDING.
            // 10. Find all developers older than 20 with more than 1 dog that contains letter 'e' in the name and print their names and job positions.

            #endregion

            #region PART 1
            Person cristofer = people
                               .FirstOrDefault(x => x.FirstName == "Cristofer");
            if (cristofer != null)
            {
                cristofer.Dogs = dogs
                                 .Where(x => x.Name == "Jack" || x.Name == "Ellie" || x.Name == "Hank" || x.Name == "Tilly")
                                 .ToList();
            }
            Person freddy = people
                            .FirstOrDefault(x => x.FirstName == "Freddy");
            if (freddy != null)
            {
                freddy.Dogs = dogs
                              .Where(x => x.Name == "Oscar" || x.Name == "Toby" || x.Name == "Chanel" || x.Name == "Bo" || x.Name == "Scout")
                              .ToList();
            }

            Person erin = people
                          .FirstOrDefault(x => x.FirstName == "Erin");
            if (erin != null)
            {
                erin.Dogs = dogs
                            .Where(x => x.Name == "Trixie" || x.Name == "Archie" || x.Name == "Max")
                            .ToList();
            }

            Person amelia = people
                            .FirstOrDefault(x => x.FirstName == "Amelia");
            if (amelia != null)
            {
                amelia.Dogs = dogs
                              .Where(x => x.Name == "Abby" || x.Name == "Shadow")
                              .ToList();
            }

            Person larry = people
                           .FirstOrDefault(x => x.FirstName == "Larry");
            if (larry != null)
            {
                larry.Dogs = dogs
                             .Where(x => x.Name == "Zoe" || x.Name == "Ollie")
                             .ToList();
            }

            Person erika = people
                           .FirstOrDefault(x => x.FirstName == "Erika");
            if (erika != null)
            {
                erika.Dogs = dogs
                             .Where(x => x.Race == Race.Retriever)
                             .ToList();
            }

            if (erin != null)
            {
                erin.Dogs.Add(dogs.Where(x => x.Name == "Chet").FirstOrDefault());
                erin.Dogs.Add(dogs.Where(x => x.Name == "Ava").FirstOrDefault());
                //erin.PrintAllDogs();
            }

            Person august = people
                            .FirstOrDefault(x => x.FirstName == "August");
            if (august != null)
            {
                august.Dogs = dogs
                              .Where(x => x.Name == "Diesel" || x.Name == "Rigby")
                              .ToList();
                //august.PrintAllDogs();
            }
            #endregion

            #region PART 2

            // 1. Find and print all persons firstnames starting with 'R', ordered by age - DESCENDING ORDER.
            List <Person> startWithRDesc = people
                                           .Where(x => x.FirstName.StartsWith("S"))
                                           .OrderByDescending(x => x.Age)
                                           .ToList();

            // 2. Find and print all brown dogs names and ages older than 3 years, ordered by age - ASCENDING ORDER.
            List <Dog> brownDogs = dogs
                                   .Where(x => x.Color == Color.Brown && x.Age > 3)
                                   .OrderBy(x => x.Age)
                                   .ToList();
            //PrintListDogs(brownDogs);

            // 3. Find and print all persons with more than 2 dogs, ordered by name - DESCENDING ORDER.
            List <Person> moreThan2Dogs = people
                                          .Where(x => x.Dogs != null && x.Dogs.Count > 2)
                                          .OrderByDescending(x => x.FirstName)
                                          .ToList();
            //PrintListPerson(moreThan2Dogs);

            // 4. Find and print all persons names, last names and job positions that have just one race type dogs.
            List <Person> oneTypeDogs = people
                                        .Where(x => x.Dogs != null && x.Dogs.Select(y => y.Race).Distinct().Count() == 1)
                                        .ToList();
            //PrintListPerson(oneTypeDogs);

            // 5. Find and print all Freddy`s dogs names older than 1 year, grouped by dogs race.

            var freddiesDogs = freddy.Dogs
                               .Where(x => x.Age > 1)
                               .GroupBy(x => x.Race)
                               .ToList();
            //foreach (var group in freddiesDogs)
            //{
            //    Console.WriteLine($"Dogs from race: {group.Key}");
            //    foreach (var dog in group)
            //    {
            //        Console.WriteLine($"{dog.Name}");
            //    }
            //}

            // 6. Find and print last 10 persons grouped by their age.
            var ageGroup = people
                           .GroupBy(x => x.Age)
                           .Reverse()
                           .Take(10)
                           .ToList();
            //foreach (var group in ageGroup)
            //{
            //    Console.WriteLine($"Age: {group.Key}");
            //    foreach (var person in group)
            //    {
            //        Console.WriteLine($"{person.FirstName} {person.LastName}");
            //    }
            //}

            // 7. Find and print all dogs names from Cristofer, Freddy, Erin and Amelia, grouped by color and ordered by name - ASCENDING ORDER.
            var dogNames = people
                           .Where(x => x.FirstName == "Cristofer" || x.FirstName == "Freddy" || x.FirstName == "Erin" || x.FirstName == "Amelia")
                           .SelectMany(x => x.Dogs)
                           .OrderBy(x => x.Name)
                           .GroupBy(x => x.Color)
                           .ToList();
            //foreach (var group in dogNames)
            //{
            //    Console.WriteLine($"Dogs with color: {group.Key}");
            //    foreach (var dog in group)
            //    {
            //        Console.WriteLine($"{dog.Name}");
            //    }
            //}

            // 8. Find and persons that have same dogs races and order them by name length ASCENDING, then by age DESCENDING.
            var sameRaces = people
                            .Where(x => x.Dogs != null)
                            .SelectMany(x => x.Dogs)
                            .GroupBy(y => y.Race)
                            .Select(x => x.Key)
                            .ToList();

            //foreach (var item in sameRaces)
            //{
            //    Console.WriteLine(item.ToString());
            //}

            var peopleWithSameRace = people
                                     .Where(x => x.Dogs != null && x.Dogs.Select(y => y.Race).Intersect(sameRaces).ToList().Count() != 0)
                                     .ToList();

            //PrintListPerson(peopleWithSameRace);


            // 9. Find the last dog of Amelia and print all dogs form other persons older than Amelia, ordered by dogs age DESCENDING.
            Dog lastAmelia = amelia.Dogs
                             .LastOrDefault();
            List <Dog> otherOlder = dogs
                                    .Where(x => x.Age > lastAmelia.Age)
                                    .OrderByDescending(x => x.Age)
                                    .ToList();
            //Console.WriteLine($"{lastAmelia.Age}");
            //PrintListDogs(otherOlder);

            // 10. Find all developers older than 20 with more than 1 dog that contains letter 'e' in the name and print their names and job positions.
            var developers = people
                             .Where(x => x.Occupation == Job.Developer && x.Age > 20)
                             .Where(x => x.Dogs != null && x.Dogs.Count() > 1)
                             .Where(x => x.Dogs.Any(y => y.Name.Contains("e")))
                             //.Where(x => x.Dogs.Where(z => z.Name.Contains("e")))
                             //or
                             //.Select(x => x.Dogs.Where(y => y.Name.Contains("e")))
                             .ToList();

            PrintListPerson(developers);
            #endregion

            Console.ReadLine();
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            var people = PersonService.GetAllPeople();

            var dogs = DogService.GetAllDogs();

            #region Exercises

            //==============================
            // TODO LINQ expressions :)
            // Your turn guys...
            //==============================

            //PART 1
            // 1. Take person Cristofer and add Jack, Ellie, Hank and Tilly as his dogs.
            Console.WriteLine("------------------------Task 1-----------------------------");
            Person cristofer = people.FirstOrDefault(x => x.FirstName == "Cristofer");
            if (cristofer != null)
            {
                //cristofer.Dogs = new List<Dog>();
                //Dog jack = dogs.FirstOrDefault(x => x.Name == "Jack");
                //cristofer.Dogs.Add(jack);
                cristofer.Dogs = dogs.Where(x => x.Name == "Jack" || x.Name == "Ellie" || x.Name == "Hank" || x.Name == "Tilly").ToList();

                cristofer.PrintAllDogs();
            }



            // 2. Take person Freddy and add Oscar, Toby, Chanel, Bo and Scout as his dogs.
            Console.WriteLine("--------------------Task 2------------------------------");
            Person freddy = people.FirstOrDefault(x => x.FirstName == "Freddy");
            if (freddy != null)
            {
                freddy.Dogs = dogs.Where(x => x.Name == "Oscar" || x.Name == "Toby" || x.Name == "Chanel" || x.Name == "Bo" || x.Name == "Scout").ToList();
                freddy.PrintAllDogs();
            }

            // 3. Add Trixie, Archie and Max as dogs from Erin.
            Console.WriteLine("--------------------Task 3------------------------------");
            Person erin = people.FirstOrDefault(x => x.FirstName == "Erin");
            if (erin != null)
            {
                erin.Dogs = dogs.Where(x => x.Name == "Trixie" || x.Name == "Archie" || x.Name == "Max").ToList();
            }
            erin.PrintAllDogs();


            // 4. Give Abby and Shadow to Amelia.
            Console.WriteLine("--------------------Task 4------------------------------");
            Person amelia = people.FirstOrDefault(x => x.FirstName == "Amelia");
            if (amelia != null)
            {
                amelia.Dogs = dogs.Where(x => x.Name.Equals("Abby") || x.Name.Equals("Shadow")).ToList();
            }

            amelia.PrintAllDogs();

            Console.WriteLine("--------------------Task 5------------------------------");
            // 5. Take person Larry and Zoe, Ollie as his dogs.
            Person larry = people.FirstOrDefault(x => x.FirstName == "Larry");
            if (larry != null)
            {
                //larry.Dogs = new List<Dog>();
                //Dog zoe = dogs.FirstOrDefault(x => x.Name == "Zoe");
                //Dog ollie = dogs.FirstOrDefault(x => x.Name == "Ollie");
                //larry.Dogs.Add(zoe);
                //larry.Dogs.Add(ollie);

                larry.Dogs = dogs.Where(x => x.Name == "Zoe" || x.Name == ("Ollie")).ToList();
                larry.PrintAllDogs();
            }
            // 6. Add all retrievers to Erika.
            Console.WriteLine("--------------------Task 6------------------------------");
            Person erika = people.FirstOrDefault(x => x.FirstName == "Erika");
            if (erika != null)
            {
                erika.Dogs = dogs.Where(x => x.Race == Race.Retriever).ToList();
            }
            erika.PrintAllDogs();


            // 7. Erin has Chet and Ava and now give Diesel to August thah previously has just Rigby
            Console.WriteLine("--------------------Task 7------------------------------");

            //Dog chet = dogs.FirstOrDefault(x => x.Name == "Chet");
            //Dog ava = dogs.FirstOrDefault(x => x.Name == "Ava");
            //erin.Dogs.Add(chet);
            //erin.Dogs.Add(ava);
            erin.Dogs.Add(dogs.Where(x => x.Name == "Chet" || x.Name == "Ava").FirstOrDefault());
            erin.PrintAllDogs();

            //PART 2 - LINQ
            Console.WriteLine("--------------------Task 1------------------------------");
            // 1. Find and print all persons firstnames starting with 'S', ordered by age - DESCENDING ORDER.
            List <Person> firstNamesStartsS = people.Where(x => x.FirstName.StartsWith("S"))
                                              .OrderByDescending(x => x.Age)
                                              .ToList();

            PrintListPerson(firstNamesStartsS);



            Console.WriteLine("--------------------Task 2------------------------------");
            // 2. Find and print all brown dogs names and ages older than 3 years, ordered by age - ASCENDING ORDER.
            List <Dog> dogByColor = dogs
                                    .Where(x => x.Color == Color.Brown && x.Age > 3)
                                    .OrderByDescending(x => x.Age).ToList();
            dogByColor.Reverse();

            PrintListDog(dogByColor);

            Console.WriteLine("--------------------Task 3------------------------------");
            // 3. Find and print all persons with more than 2 dogs, ordered by name - DESCENDING ORDER.
            List <Person> personDogs = people
                                       .Where(x => (x.Dogs != null) && (x.Dogs.Count() > 2))
                                       .OrderByDescending(x => x.FirstName)
                                       .ToList();
            PrintListPerson(personDogs);

            Console.WriteLine("--------------------Task 4------------------------------");
            // 4. Find and print all persons names, last names and job positions that have just one race type dogs.
            List <Person> personTask4 = people.Where(x => x.Dogs != null && x.Dogs.Select(y => y.Race).Distinct().Count() == 1).ToList();
            PrintListPerson(personTask4);


            Console.WriteLine("--------------------Task 5------------------------------");
            // 5. Find and print all Freddy`s dogs names older than 1 year, grouped by dogs race.

            var freddyDogs = freddy.Dogs.Where(x => x.Age > 1).GroupBy(x => x.Race).ToList();
            foreach (var group in freddyDogs)
            {
                Console.WriteLine($"{group.Key}");
                foreach (var dog in group)
                {
                    Console.WriteLine($"{dog.Name}");
                }
            }

            Console.WriteLine("--------------------Task 6------------------------------");
            // 6. Find and print last 10 persons grouped by their age.

            people.Reverse();
            var lastTenPersons = people

                                 .Take(10)
                                 .GroupBy(x => x.Age)
                                 .Reverse()
                                 .ToList();

            foreach (var group in lastTenPersons)
            {
                Console.WriteLine($"Age : {group.Key}");
                foreach (var person in group)
                {
                    Console.Write($"{person.FirstName} {person.LastName} \n");
                }
            }



            Console.WriteLine("--------------------Task 7------------------------------");
            // 7. Find and print all dogs names from Cristofer, Freddy, Erin and Amelia, grouped by color and ordered by name - ASCENDING ORDER.
            var dog7 = people
                       .Where(x => x.FirstName == "Cristofer" || x.FirstName == "Freddy" || x.FirstName == "Erin" || x.FirstName == "Amelia")
                       .SelectMany(x => x.Dogs)
                       .OrderBy(x => x.Name)
                       .GroupBy(x => x.Color)
                       .ToList();



            foreach (var dog in dog7)
            {
                Console.WriteLine($"Dog color: {dog.Key}");
                foreach (var item in dog)
                {
                    Console.WriteLine($"Name :{item.Name}");
                }
            }



            Console.WriteLine("--------------------Task 8------------------------------");
            // 8. Find and persons that have slisame dogs races and order them by name length ASCENDING, then by age DESCENDING.
            var person8 = people
                          .Select(x => x.Dogs)
                          .GroupBy(y => y.Race)
                          .Distinct()


                          .ToList();



            Console.WriteLine("--------------------Task 9------------------------------");
            //9. Find the last dog of Amelia and print all dogs form other persons older than Amelia, ordered by dogs age DESCENDING.

            Dog lastDog = amelia.Dogs
                          .LastOrDefault();
            var person9 = people
                          .Where(x => x.Age > amelia.Age).ToList();
            //PrintListPerson(person9);
            var dog9 = person9
                       .Where(x => x.Dogs != null)
                       .SelectMany(x => x.Dogs)
                       .OrderByDescending(x => x.Age)
                       .ToList();
            Console.WriteLine($"{lastDog.Name} {lastDog.Age}");
            PrintListDog(dog9);


            Console.WriteLine("--------------------Task 10------------------------------");
            //10.Find all developers older than 20 with more than 1 dog that contains letter 'e' in the name and print their names and job positions.
            var developers = people
                             .Where(x => x.Dogs != null)
                             .Where(x => (x.Occupation == Job.Developer) && x.Age > 20)
                             .Where(x => x.Dogs.Count() > 1)
                             .Where(x => x.Dogs.Any(y => y.Name.Contains("e")))
                             .ToList();


            PrintListPerson(developers);



            Console.ReadLine();
            #endregion
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            var people = PersonService.GetAllPeople();

            var dogs = DogService.GetAllDogs();

            #region Exercises

            //==============================
            // TODO LINQ expressions :)
            // Your turn guys...
            //==============================

            //PART 1
            // 1. Take person Cristofer and add Jack, Ellie, Hank and Tilly as his dogs.
            Person cristofer = people.FirstOrDefault(x => x.FirstName.Equals("Cristofer"));
            if (cristofer != null)
            {
                cristofer.Dogs = dogs
                                 .Where(x => x.Name.Equals("Jack") ||
                                        x.Name.Equals("Ellie") ||
                                        x.Name.Equals("Tilly") ||
                                        x.Name.Equals("Hank"))
                                 .ToList();
            }

            // 2. Take person Freddy and add Oscar, Toby, Chanel, Bo and Scout as his dogs.

            Person freddy = people.FirstOrDefault(x => x.FirstName.Equals("Freddy"));
            if (freddy != null)
            {
                freddy.Dogs = dogs
                              .Where(x => x.Name.Equals("Oscar") ||
                                     x.Name.Equals("Toby") ||
                                     x.Name.Equals("Bo") ||
                                     x.Name.Equals("Scout"))
                              .ToList();
            }

            // 3. Add Trixie, Archie and Max as dogs from Erin.
            Person erin = people.FirstOrDefault(x => x.FirstName.Equals("Erin"));
            if (erin != null)
            {
                erin.Dogs = dogs
                            .Where(x => x.Name.Equals("Trixie") ||
                                   x.Name.Equals("Archie") ||
                                   x.Name.Equals("Max"))
                            .ToList();
            }

            // 4. Give Abby and Shadow to Amelia.

            Person amelia = people.FirstOrDefault(x => x.FirstName.Equals("Amelia"));
            if (amelia != null)
            {
                amelia.Dogs = dogs
                              .Where(x => x.Name.Equals("Abby") ||
                                     x.Name.Equals("Shadow"))
                              .ToList();
            }
            // 5. Take person Larry and Zoe, Ollie as his dogs.

            Person Larry = people.FirstOrDefault(x => x.FirstName.Equals("Larry"));
            if (Larry != null)
            {
                Larry.Dogs = dogs
                             .Where(x => x.Name.Equals("Zoe") ||
                                    x.Name.Equals("Ollie"))
                             .ToList();
            }
            // 6. Add all retrievers to Erika.
            Person erika = people.FirstOrDefault(x => x.FirstName.Equals("Erika"));
            if (erika != null)
            {
                erika.Dogs = dogs
                             .Where(x => x.Race == Race.Retriever)
                             .ToList();
            }

            // 7. Erin has Chet and Ava and now give Diesel to August thah previously has just Rigby

            //PART 2 - LINQ
            // 1. Find and print all persons firstnames starting with 'R', ordered by age - DESCENDING ORDER.
            List <Person> firstName = people
                                      .Where(x => x.FirstName.StartsWith("R"))
                                      .OrderByDescending(x => x.Age)
                                      .ToList();

            // 2. Find and print all brown dogs names and ages older than 3 years, ordered by age - ASCENDING ORDER.
            List <Dog> braonDog = dogs
                                  .Where(x => x.Color == Color.Brown & x.Age >= 3)
                                  .OrderBy(x => x.Age)
                                  .ToList();

            // 3. Find and print all persons with more than 2 dogs, ordered by name - DESCENDING ORDER.
            List <Person> hasTwoDogs = people.Where(x => x.Dogs != null && x.Dogs.Count > 2)
                                       .OrderBy(x => x.Dogs.Count)
                                       .ToList();

            // 4. Find and print all persons names, last names and job positions that have just one race type dogs.
            List <Person> oneRace = people.Where(x => x.Dogs != null &&
                                                 x.Dogs.Select(y => y.Race)
                                                 .Distinct()
                                                 .Count() == 1)
                                    .ToList();

            // 5. Find and print all Freddy`s dogs names older than 1 year, grouped by dogs race.

            Person fredy      = people.FirstOrDefault(x => x.FirstName.Equals("Freddy"));
            var    fredysDogs = fredy.Dogs.Where(x => x.Age > 1).GroupBy(x => x.Race).ToList();

            foreach (var item in fredysDogs)
            {
                foreach (var item1 in item)
                {
                    System.Console.WriteLine(item1.Name);
                }
            }

            // 6. Find and print last 10 persons grouped by their age.
            // 7. Find and print all dogs names from Cristofer, Freddy, Erin and Amelia, grouped by color and ordered by name - ASCENDING ORDER.
            // 8. Find and persons that have same dogs races and order them by name length ASCENDING, then by age DESCENDING.
            // 9. Find the last dog of Amelia and print all dogs form other persons older than Amelia, ordered by dogs age DESCENDING.
            // 10. Find all developers older than 20 with more than 1 dog that contains letter 'e' in the name and print their names and job positions.

            #endregion
        }