LINQ查询操作符

    #region Types



    public enum Countries

    {

        USA,

        Italy,

    }



    public class Customer

    {

        public string Name {get;set;}

        public string City { get; set; }

        public Countries Country { get; set; }

        public Order[] Orders { get; set; }



        public override string ToString()

        {

            return String.Format("Name: {0} - City: {1} - Country: {2}",

                this.Name, this.City, this.Country);

        }

    }



    public class Order

    {

        public int IdOrder { get; set; }

        public int Quantity { get; set; }

        public bool Shipped { get; set; }

        public string Month { get; set; }

        public int IdProduct { get; set; }



        public override string ToString()

        {

            return String.Format("IdOrder: {0} - IdProduct: {1} - Quantity: {2} - Shipped: {3} - Month: {4}",

                this.IdOrder, this.IdProduct, this.Quantity, this.Shipped, this.Month);

        }

    }



    public class Product

    {

        public int IdProduct { get; set; }

        public decimal Price { get; set; }



        public override string ToString()

        {

            return String.Format("IdProduct: {0} - Price: {1}", this.IdProduct, this.Price);

        }

    }



    #endregion



    class Program

    {

        private static Customer[] customers;

        private static Product[] products;



        static void LoadData()

        {

            customers = new Customer[] {

                new Customer {Name = "Paolo", City = "Brescia", Country = Countries.Italy, Orders = new Order[] {

                    new Order {IdOrder = 1, Quantity = 3, IdProduct = 1, Shipped = false, Month = "January"},

                    new Order {IdOrder = 2, Quantity = 5, IdProduct = 2, Shipped = true, Month = "May"}}},

                new Customer {Name = "Marco", City = "Torino", Country = Countries.Italy, Orders =  new Order[] {

                    new Order {IdOrder = 3, Quantity = 10, IdProduct = 1, Shipped = false, Month = "July"},

                    new Order {IdOrder = 4, Quantity = 20, IdProduct = 3, Shipped = true, Month = "December"}}},

                new Customer {Name = "James", City = "Dallas", Country = Countries.USA, Orders =  new Order[] {

                    new Order {IdOrder = 5, Quantity = 20, IdProduct = 3, Shipped = true, Month = "December"}}},

                new Customer {Name = "Frank", City = "Seattle", Country = Countries.USA, Orders =  new Order[] {

                    new Order {IdOrder = 6, Quantity = 20, IdProduct = 5, Shipped = false, Month = "July"}}}};



            products = new Product[] {

                new Product {IdProduct = 1, Price = 10 },

                new Product {IdProduct = 2, Price = 20 },

                new Product {IdProduct = 3, Price = 30 },

                new Product {IdProduct = 4, Price = 40 },

                new Product {IdProduct = 5, Price = 50 },

                new Product {IdProduct = 6, Price = 60 }};

        }



        static Program()

        {

            LoadData();

        }

        static void Main(string[] args)

        {

           // restrictionOperatorWithIndex();



           // projectionWithIndex();

            //selectManySample();

            selectManySampleWithCustomSelector();

        }



        //使用分页的Where

        static void restrictionOperatorWithIndex()

        {

            var expr = customers

                .Where((c, index) => (c.Country == Countries.Italy && index >= 1))

                .Select(c => new { c.Name });



            foreach (var item in expr)

            {

                Console.WriteLine(item);

            }

        }



        //使用分页的Select

        static void projectionWithIndex()

        {

            var expr =

                customers

                .Select((c, index) => new { index, c.Name, c.Country });



            foreach (var item in expr)

            {

                Console.WriteLine(item);

            }

        }



        //SelectMany

        static void selectManySample()

        {

            var orders = customers

                .Where(c => c.Country == Countries.Italy)

                .SelectMany(c =>c.Orders);



            //查询表达式

            //var orders = from c in customers

            //             where c.Country == Countries.Italy

            //             from o in c.Orders

            //             select o;

            foreach (var item in orders)

            {

                Console.WriteLine(item);

            }

        }





        static void selectManySampleWithCustomSelector()

        {

            var items = customers

                .Where(c => c.Country == Countries.Italy)

                .SelectMany(c => c.Orders, (c, o) => new { o.Quantity, o.IdProduct });



            //查询表达式

            //var items = from c in customers

            //            where c.Country == Countries.Italy

            //            from o in c.Orders

            //            select new { o.Quantity, o.IdProduct };

            foreach (var item in items)

            {

                Console.WriteLine(item);

            }

        }



    }

 

你可能感兴趣的:(LINQ)