示例#1
0
        public void RunNewExamples()
        {
            IEnumerable <WebPage> webPages          = DemoCollections.GetWebPages();
            List <PageComponent>  allPageComponents = webPages.SelectMany(wps => wps.PageComponents).ToList();
            //allPageComponents.ForEach(pc => Console.WriteLine(pc.ToString()));
            List <char> chars = webPages.SelectMany(wps => wps.Name).ToList();
            //chars.ForEach(c => Console.Write(c));

            var uniqueExtensions = webPages.SelectMany(wps => wps.PageComponents
                                                       .Select(pc => new { Extension = pc.Extension })).Distinct().OrderBy(n => n.Extension).ToList();
            //uniqueExtensions.ForEach(p => Console.WriteLine(p.Extension));

            var allPageComponentsWithIndex = webPages.SelectMany
                                                 ((wps, index) => wps.PageComponents.Select(c => new { ComponentName = c.Name, PositionOfContainer = index })).ToList();
            //allPageComponentsWithIndex.ForEach(pc => Console.WriteLine(string.Concat(pc.PositionOfContainer, ": ", pc.ComponentName)));
            var pagesWithComponents = webPages
                                      .SelectMany(wps => wps.PageComponents,
                                                  (wp, cp) => new { PageName = wp.Name, ComponentName = cp.Name }).ToList();
            //pagesWithComponents.ForEach(p => Console.WriteLine(string.Concat(p.PageName, ": ", p.ComponentName)));

            var pagesWithComponentsAndIndex = webPages.SelectMany((wps, index) => wps.PageComponents
                                                                  .Select(c => new { ComponentName = c.Name, PositionOfContainer = index }),
                                                                  (wp, cp) => new { PageName = wp.Name, ComponentName = cp.ComponentName, PositionOfContainer = cp.PositionOfContainer }).ToList();

            pagesWithComponentsAndIndex.ForEach(c => Console.WriteLine(string.Concat("Page: ", c.PageName, ", position: ", c.PositionOfContainer, ", component: ", c.ComponentName)));
        }