25LINQ拾遗及实例

  投影

□ 遍历数组索引,Select获取

            int[] indexes = {0, 2}; 
            string[] strs = {"a", "b", "c", "d"};
 
            var result = from i in indexes 
                select strs[i];
 
            foreach (string str in result) 
            { 
                Console.Write(str + " "); 
            } 
            Console.ReadKey();

结果:a c

 

□ 使用集合索引投影匿名对象

        static void Main(string[] args) 
        { 
            int[] numbers = {0, 3, 4, 5, 7}; 
            var result = numbers.Select((n, index) => new {Num = n, IsValueEqualIndex = (n == index)}); 
            foreach (var item in result) 
            { 
                Console.WriteLine("{0}:{1}",item.Num,item.IsValueEqualIndex); 
            } 
            Console.ReadKey(); 
        }

结果:
1

 

□ 2个集合的笛卡尔积

            int[] numberA = {0,2}; 
            int[] numberB = {1,3};
 
            var result = from a in numberA 
                from b in numberB 
                select new {a, b}; 
            foreach (var item in result) 
            { 
                Console.WriteLine("{0},{1}",item.a,item.b); 
            } 
            Console.ReadKey();

结果:
2

 

□ 笛卡尔积应用,列出所有客户的所有订单

   public class Customer 
    { 
        public int Id { get; set; } 
        public string Name { get; set; } 
        public List<Order> Orders { get; set; } 
    }
 
    public class Order 
    { 
        public int Id { get; set; } 
        public decimal Total { get; set; } 
    }
 
            Order order1 = new Order(){Id = 1,Total = 10.00m}; 
            Order order2 = new Order(){Id = 2,Total = 20.00m}; 
            Order order3 = new Order(){Id = 3,Total = 30.00m};
 
            List<Customer> customers = new List<Customer>() 
            { 
                new Customer() 
                { 
                    Id = 1, 
                    Name = "张三", 
                    Orders = new List<Order>(){order1,order2} 
                }, 
                new Customer() 
                { 
                    Id = 2, 
                Name = "李四", 
                Orders = new List<Order>() { order3 } 
                } 
            };
 
 
            var result = from c in customers 
                from o in c.Orders 
                select new {c.Id, c.Name, o.Total};
 
            foreach (var item in result) 
            { 
                Console.WriteLine("客户编号:{0},姓名:{1},订单金额:{2}", item.Id,item.Name,item.Total); 
            }
 
            Console.ReadKey();    
 

结果:
3

 

  分组

□ group n by 条件 into g, g.Key就是分组标准和条件, g代表分组里所有元素的集合

            int[] numbers = { 5, 4, 1, 3, 9, 8, 6}; 
            var result = from n in numbers 
                group n by n%5 into g 
                select new {Remainder = g.Key, Numbers = g};
 
            foreach (var item in result) 
            { 
                Console.WriteLine("元素除以5的余数为:{0}",item.Remainder); 
                foreach (var n in item.Numbers) 
                { 
                    Console.WriteLine(n); 
                } 
            } 
            Console.ReadKey();

结果:
4

 

  连接

□ 根据2个集合的相同部分连接

    public class Product 
    { 
        public int Id { get; set; } 
        public string Name { get; set; } 
        public string Category { get; set; } 
    }
 
            List<Product> products = new List<Product>() 
            { 
                new Product(){Id=1,Name = "体恤衫",Category = "服饰"}, 
                new Product(){Id = 2,Name = "香水",Category = "化妆品"} 
            };
 
            string[] categoreis = new string[] 
            { 
                "服饰", 
                "化妆品", 
                "电子" 
            };
 
            var result = from c in categoreis 
                join p in products on c equals p.Category 
                select new {Category = c, p.Name}; 
            foreach (var item in result) 
            { 
                Console.WriteLine(item.Name + "属于:" + item.Category); 
            } 
            
            Console.ReadKey();   
 

结果:
5

 

□ 把其中一个集合的所有元素放到一个别名集合中

            List<Product> products = new List<Product>() 
            { 
                new Product(){Id=1,Name = "体恤衫",Category = "服饰"}, 
                new Product(){Id = 2,Name = "香水",Category = "化妆品"} 
            };
 
            string[] categoreis = new string[] 
            { 
                "服饰", 
                "化妆品", 
                "电子" 
            };
 
            var result = from c in categoreis 
                join p in products on c equals p.Category into ps 
                select new {Category = c, Products = ps}; 
            foreach (var item in result) 
            { 
                Console.WriteLine("类别:{0}包括:",item.Category); 
                foreach (var p in item.Products) 
                { 
                    Console.WriteLine("      " + p.Name); 
                } 
            } 
            
            Console.ReadKey();

结果:
6

posted @ 2014-04-06 00:43  Darren Ji  阅读(394)  评论(2编辑  收藏  举报

我的公众号:新语新世界,欢迎关注。