您的位置:首页 > 编程学习 > ASP.NET > 正文

linq中join用法

更多 时间:2014-4-20 类别:编程学习 浏览量:3696

linq中join用法

linq中join用法

一、linq中join方法原型

  •  
  • C# 代码   复制
  • 
    public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>( 
        this IEnumerable<TOuter> outer, 
        IEnumerable<TInner> inner, 
        Func<TOuter, TKey> outerKeySelector, 
        Func<TInner, TKey> innerKeySelector, 
        Func<TOuter, TInner, TResult> resultSelector)
    
    		
  •  

    二、先创建linq中join方法的实验用例

  •  
  • C# 代码   复制
  • 
        class Customer 
        { 
            public int CustomerId { get; set; } 
            public string Name { get; set; } 
            public int Age { get; set; } 
        } 
        class Product 
        { 
            public int ProductId { get; set; } 
            public string Name { get; set; } 
            public string Origin { get; set; } 
        } 
        class Order 
        { 
            public int OrderId { get; set; } 
            public int CustomerId { get; set; } 
            public List<Product> Products { get; set; } 
        } 
        static List<Customer> customers; 
        static List<Product> products; 
        static List<Order> orders; 
        public static void CreateEntities() 
        { 
            customers = new List<Customer>{ 
                new Customer(){ CustomerId = 1, Name = "CA", Age=13}, 
                new Customer(){ CustomerId = 2, Name = "CB", Age=13}, 
                new Customer(){ CustomerId = 3, Name = "CC", Age=13}, 
                new Customer(){ CustomerId = 4, Name = "CD", Age=13} 
            }; 
            products = new List<Product>{ 
                new Product(){ ProductId = 1, Name = "PA", Origin="P1" }, 
                new Product(){ ProductId = 2, Name = "PB", Origin="P2" }, 
                new Product(){ ProductId = 3, Name = "PC", Origin="P1" }, 
                new Product(){ ProductId = 4, Name = "PD", Origin="P3" } 
            }; 
            orders = new List<Order>{ 
                new Order(){ OrderId = 1 , CustomerId =1, 
                    Products = new List<Product>{ 
                        new Product(){ ProductId = 2, Name = "PB", Origin="P2" }, 
                        new Product(){ ProductId = 3, Name = "PC", Origin="P1" } 
                }}, 
                new Order(){ OrderId = 2 , CustomerId =1, 
                    Products = new List<Product>{ 
                        new Product(){ ProductId = 3, Name = "PC", Origin="P1" }, 
                        new Product(){ ProductId = 4, Name = "PD", Origin="P3" } 
                }}, 
                new Order(){ OrderId = 3 , CustomerId =3, 
                    Products = new List<Product>{ 
                        new Product(){ ProductId = 4, Name = "PD", Origin="P3" } 
                }}, 
                new Order(){ OrderId = 4 , CustomerId =2, 
                    Products = new List<Product>
      
    标签:linq linq join