Exemplo n.º 1
0
        public void JoinLists()
        {
            var programmingLanguages     = ProgrammingLanguageRepository.GetProgrammingLanguages().ToList();
            var programmingLanguageTypes = ProgrammingLanguageTypeRepository.GetProgrammingLanguageTypes().ToList();

            /* Join */
            // Allows to combine the desired data into one query and tranform the results into a new Type
            // The caller(programmingLanguages) is considered the outer list
            // The parameter(programmingLanguageTypes) is considered the inner list

            /*
             * Parameters:
             * - outerKeySelector: Key selector that is used to match the columns on the join from the outer list
             * - innerKeySelector: Key selector that is used to match the columns on the join from the inner list
             * - resultSelector: Select delegate used for projection
             */
            var programmingLanguagesAndTypes = programmingLanguages.Join(programmingLanguageTypes, pl => pl.TypeId, plt => plt.TypeId, (pl, plt) => new
            {
                pl.Name, plt.Type
            }).ToList();

            Assert.AreEqual(programmingLanguagesAndTypes.First().Name, "C#");
            Assert.AreEqual(programmingLanguagesAndTypes.First().Type, "Object Oriented");
            Assert.AreEqual(programmingLanguagesAndTypes.Last().Name, "Ruby");
            Assert.AreEqual(programmingLanguagesAndTypes.First().Type, "Object Oriented");
        }
Exemplo n.º 2
0
        public void GroupingByParentData()
        {
            // GroupBy (Parent Property)

            var programmingLanguages     = ProgrammingLanguageRepository.GetProgrammingLanguages().ToList();
            var programmingLanguageTypes = ProgrammingLanguageTypeRepository.GetProgrammingLanguageTypes().ToList();


            var programmingLanguagesTypeQuery = programmingLanguages.Join(programmingLanguageTypes, pg => pg.TypeId,
                                                                          pgt => pgt.TypeId, (pl, plt) => new
            {
                // We save an instance of the programming language to access the properties on the future
                ProgrammingLanguageInstance = pl,
                ProgrammingLanguageType     = plt
            });


            // Getting the Market share of programming languages, Ordering by programming language Type name
            var programmingLanguagesMarketShare = programmingLanguagesTypeQuery.GroupBy(
                pg => pg.ProgrammingLanguageType, pg => pg.ProgrammingLanguageInstance.MarketShare, (groupKey, marketShareTotal) => new
            {
                Key         = groupKey.Type,
                MarketShare = marketShareTotal.Sum()
            }).ToList();

            // Three results (total):
            Assert.AreEqual(programmingLanguagesMarketShare.First().Key, "Object Oriented");
            Assert.AreEqual(programmingLanguagesMarketShare.First().MarketShare, 77);

            Assert.AreEqual(programmingLanguagesMarketShare[1].Key, "Imperative");
            Assert.AreEqual(programmingLanguagesMarketShare[1].MarketShare, 4);

            Assert.AreEqual(programmingLanguagesMarketShare.Last().Key, "Functional");
            Assert.AreEqual(programmingLanguagesMarketShare.Last().MarketShare, 19);
        }