/*
    Projektions-Operatoren
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _00LINQBasics
{
    public class Customer
    {
        public string customerID;
        public string companyName;
        public string country;

        public Order[] orders;
    }

    public class Order
    {
        public int orderID;
        public DateTime orderDate;
        public string shipCountry;

        public int[] productID;
    }

    public class Product
    {
        public int productID;
        public double unitPrice;
        public string name;
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            Product p1 = new Product { productID = 31, unitPrice = 12.5, name = "Gorgonzola Telino" };
            Product p2 = new Product { productID = 52, unitPrice = 7.0, name = "Filo Mix" };
            Product p3 = new Product { productID = 63, unitPrice = 43.9, name = "Vegie-spread" };
            Product p4 = new Product { productID = 48, unitPrice = 12.7, name = "Chocolade" };
            Product p5 = new Product { productID = 43, unitPrice = 46.0, name = "Ipoh Coffee" };
            Product p6 = new Product { productID = 37, unitPrice = 26.0, name = "Gravad lax" };

            //Orders
            Order[] orderALFKI = new Order[]
                {
                    new Order {orderID = 10643, orderDate = DateTime.Parse("25.08.1997"), shipCountry = "Germany", productID = new int[]{31,37}},
                    new Order {orderID = 10692, orderDate = DateTime.Parse("03.10.1997"), shipCountry = "Germany", productID = new int[]{63}},
                    new Order {orderID = 10702, orderDate = DateTime.Parse("13.10.1997"), shipCountry = "Germany", productID = new int[]{48,52,43}},
                };

            Order[] orderBERGS = new Order[]
                {
                    new Order {orderID = 10278, orderDate = DateTime.Parse("12.08.1996"), shipCountry = "Sweden", productID = new int[]{48,37}},
                    new Order {orderID = 10280, orderDate = DateTime.Parse("14.08.1996"), shipCountry = "Sweden", productID = new int[]{52,43}},
                    new Order {orderID = 10384, orderDate = DateTime.Parse("16.12.1996"), shipCountry = "Sweden", productID = new int[]{48,37}},
                };

            Order[] orderDUMON = new Order[]
                {
                    new Order {orderID = 10311, orderDate = DateTime.Parse("20.09.1996"), shipCountry = "France", productID = new int[]{37,52}},
                    new Order {orderID = 10609, orderDate = DateTime.Parse("24.07.1997"), shipCountry = "France", productID = new int[]{31,3743}},
                    new Order {orderID = 10683, orderDate = DateTime.Parse("26.09.1997"), shipCountry = "France", productID = new int[]{43,37,52}},
                };

            Order[] orderLEHMS = new Order[]
                {
                    new Order {orderID = 10279, orderDate = DateTime.Parse("13.08.1996"), shipCountry = "Germany", productID = new int[]{37,52}},
                    new Order {orderID = 10284, orderDate = DateTime.Parse("19.08.1996"), shipCountry = "Germany", productID = new int[]{43,52,63}},
                    new Order {orderID = 10343, orderDate = DateTime.Parse("31.10.1996"), shipCountry = "Germany", productID = new int[]{43,48}},
                };

            //Customers
            Customer[] customers = new Customer[]
                {
                    new Customer {customerID = "ALFKI", companyName = "Alfreds Futterkiste", country = "Germany", orders = orderALFKI},
                    new Customer {customerID = "BERGS", companyName = "Berglunds snabbköp", country = "Sweden", orders = orderBERGS},
                    new Customer {customerID = "DUMON", companyName = "Du monde entier", country = "France", orders = orderDUMON},
                    new Customer {customerID = "LEHMS", companyName = "Lehmanns Marktstand", country = "Germany", orders = orderLEHMS}
                };

            //Codefragment//

            #region Select-Operator
            var result1 =                  // über anonymen Typ
                from c in customers
                select new { c.customerID };

            foreach (var item in result1)
            {
                Console.WriteLine(item.ToString());
                Console.WriteLine(item.customerID);
            }

                //var result2 = customers //über Lambda-Ausdruck
                //    .Select(c => c.customerID);

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

                //var result3 = // einfache Abfrage
                //    from c in customers
                //    select c.customerID;

                //foreach (var item in result3)
                //{
                //    Console.WriteLine(item);
                //}
            #endregion

            Console.WriteLine();

            #region SelectMany-Operator
            var orders1 =                      //einfache Abfrage
                from c in customers
                where c.country == "Germany"
                from o in c.orders
                select o;

            foreach (var item in orders1)
            {
                Console.WriteLine(item.orderID);
            }

                //var orders2 = customers
                //    .Where(c => c.country == "Germany")
                //    .Select(c => c.orders);

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

                //var orders3 = customers
                //    .Where(c => c.country == "Germany")
                //    .SelectMany(c => c.orders);

                //foreach (var item in orders3)
                //{
                //    Console.WriteLine(item.orderID);
                //}
            #endregion

            Console.ReadLine();
        }
    }
}