LINQ学習ノート(04)


orderedbyはselectの結果をソートするために使用されます.
concatは2つのセットを接続するために使用されます
 class CalendarEvent
    {
        public string Title { get; set; }
        public DateTimeOffset StartTime { get; set; }
        public TimeSpan Duration { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<CalendarEvent> events = new List<CalendarEvent>
            {
                new CalendarEvent
                {
                    Title = "Swing Dancing at the South Bank",
                    StartTime = new DateTimeOffset(2009,7,11,15,0,0, TimeSpan.Zero),
                    Duration = TimeSpan.FromHours(4)
                },
                
                new CalendarEvent
                {
                    Title = "Saturday Night Swing",
                    StartTime = new DateTimeOffset(2009, 7, 11, 19, 30, 0, TimeSpan.Zero),
                    Duration = TimeSpan.FromHours(6.5),
                },

                new CalendarEvent
                {
                    Title = "Formula 1 German Grand Prix",
                    StartTime = new DateTimeOffset(2009,7,12,12,19,0, TimeSpan.Zero),
                    Duration = TimeSpan.FromHours(3)
                },

                new CalendarEvent
                {
                    Title = "Swing Dance Picnic",
                    StartTime = new DateTimeOffset(2009,7,12,15,0,0,TimeSpan.Zero),
                    Duration = TimeSpan.FromHours(4)
                },

                new CalendarEvent
                {
                   Title = "Stompin' at the 100 Club",
                   StartTime = new DateTimeOffset(2009,7,13,19,45,0,TimeSpan.Zero),
                   Duration = TimeSpan.FromHours(5)
                }
            };

            var eventsBystartTime = from ev in events
                                    orderby ev.StartTime   // default ascending
                                    select ev;

            foreach (CalendarEvent ev in eventsBystartTime)
            {
                Console.WriteLine("-------------------------------");
                Console.WriteLine(ev.Title);
                Console.WriteLine(ev.StartTime);
                Console.WriteLine(ev.Duration);
            }

            var eventsBystartTimeD = from ev in events
                                    orderby ev.StartTime descending
                                    select ev;

            foreach (CalendarEvent ev in eventsBystartTimeD)
            {
                Console.WriteLine("-------------------------------");
                Console.WriteLine(ev.Title);
                Console.WriteLine(ev.StartTime);
                Console.WriteLine(ev.Duration);
            }

            var eventsByStartTimeNoDate = from ev in events
                                          orderby ev.StartTime.TimeOfDay
                                          select ev;
            foreach (CalendarEvent ev in eventsByStartTimeNoDate)
            {
                Console.WriteLine("-------------------------------");
                Console.WriteLine(ev.Title);
                Console.WriteLine(ev.StartTime);
                Console.WriteLine(ev.Duration);
            }


            var eventsByTwoConditions = from ev in events
                                        orderby ev.StartTime.Date, ev.Duration
                                        select ev;

            //var eventsByTwoConditions = events.OrderBy(ev => ev.StartTime).ThenBy(ev => ev.Duration);
            foreach (CalendarEvent ev in eventsByTwoConditions)
            {
                Console.WriteLine("-------------------------------");
                Console.WriteLine(ev.Title);
                Console.WriteLine(ev.StartTime);
                Console.WriteLine(ev.Duration);
            }

            var allEvents = eventsBystartTime.Concat(eventsBystartTimeD);
            foreach (CalendarEvent ev in allEvents)
            {
                Console.WriteLine("-------------------------------");
                Console.WriteLine(ev.Title);
                Console.WriteLine(ev.StartTime);
                Console.WriteLine(ev.Duration);
            }

        }
    }