LINQ学习——JOIN

一、JOIN的作用

     1、使用联接来结合两个或更多的集合的数据。

     2、联接操作接受两个集合然后创建一个临时的对象集合,每一个对象包含原始集合对象中的所有字段。

     Note:这里是包含而不是这个原实集合的字段一定要使用,这要看SELECT原始集合的哪些字段。

二、LINQ表达式的语法

     Jion Identifier in Collection2 On Field1 equals Field2

     Note:使用上下文关键字“equals”来比较字段,不能用“==”这个运算符

    示例:Student.cs

     

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace LINQDemoWinForm
 8 {
 9     class Student
10     {
11         public int ID { get;set;}
12         public string SName { get; set; }
13         public int Age { get; set; }
14     }
15 }
View Code

          Product.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace LINQDemoWinForm
 8 {
 9     class Product
10     {
11         public int ID { get; set; }
12         public string PName { get; set; }
13         public double Price { get; set; }
14     }
15 }
View Code

         LINQ语句

 private void button1_Click(object sender, EventArgs e)
        {
            //初始化Student数组
            Student[] arrStu = new Student[]{
                new Student{ID=1,SName="zhangsan",Age=20},
                new Student{ID=2,SName="lisi",Age=21},
                new Student{ID=3,SName="wangwu",Age=23},
                new Student{ID=4,SName="liuliu",Age=24},
            };
            //初始化Product数组
            Product[] arrPro = new Product[]{
                new Product{ID=1,PName="Apple",Price=2.25},
                new Product{ID=2,PName="Orange",Price=5.25},
                new Product{ID=3,PName="Banana",Price=7.5},
                new Product{ID=4,PName="StrawBerry",Price=6.5},
            };
            //LINQ语句
            var query = from sItem in arrStu
                        join pItem in arrPro
                        on sItem.ID equals pItem.ID
                        select new { sItem.SName, sItem.Age, pItem.PName, pItem.Price };//Select后面接一个匿名对象 
            StringBuilder sbRes = new StringBuilder();
            
            //打印
            foreach (var item in query)
            {
                sbRes.AppendFormat("SName:{0},Age:{1},PName:{2},Price:{3}", item.SName, item.Age, item.PName, item.Price);
                sbRes.AppendLine();
            }
            MessageBox.Show(sbRes.ToString());
        }
View Code

       执行结果:

             

三、标准查询运算符—Join

     原函数:

 1  //
 2         // 摘要: 
 3         //     基于匹配键对两个序列的元素进行关联。 使用默认的相等比较器对键进行比较。
 4         //
 5         // 参数: 
 6         //   outer:
 7         //     要联接的第一个序列。
 8         //
 9         //   inner:
10         //     要与第一个序列联接的序列。
11         //
12         //   outerKeySelector:
13         //     用于从第一个序列的每个元素提取联接键的函数。
14         //
15         //   innerKeySelector:
16         //     用于从第二个序列的每个元素提取联接键的函数。
17         //
18         //   resultSelector:
19         //     用于从两个匹配元素创建结果元素的函数。
20         //
21         // 类型参数: 
22         //   TOuter:
23         //     第一个序列中的元素的类型。
24         //
25         //   TInner:
26         //     第二个序列中的元素的类型。
27         //
28         //   TKey:
29         //     键选择器函数返回的键的类型。
30         //
31         //   TResult:
32         //     结果元素的类型。
33         //
34         // 返回结果: 
35         //     一个具有 TResult 类型元素的 System.Collections.Generic.IEnumerable<T>,这些元素是通过对两个序列执行内部联接得来的。
36         //
37         // 异常: 
38         //   System.ArgumentNullException:
39         //     outer 或 inner 或 outerKeySelector 或 innerKeySelector 或 resultSelector 为 null。
40         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);
View Code

 Note:这是一个泛型的扩展方法。outerKeySelector,innerKeySelector这两个参数是两个集合需要比较的键。resultSelector相当于前面的Selcet,后面可以接一个匿名对象。

    示例:

       使用到前面的Student,Product类,运行结果和前面一样。

 1 private void button1_Click_1(object sender, EventArgs e)
 2         {
 3             //初始化Student数组
 4             Student[] arrStu = new Student[]{
 5                 new Student{ID=1,SName="zhangsan",Age=20},
 6                 new Student{ID=2,SName="lisi",Age=21},
 7                 new Student{ID=3,SName="wangwu",Age=23},
 8                 new Student{ID=4,SName="liuliu",Age=24},
 9             };
10             //初始化Product数组
11             Product[] arrPro = new Product[]{
12                 new Product{ID=1,PName="Apple",Price=2.25},
13                 new Product{ID=2,PName="Orange",Price=5.25},
14                 new Product{ID=3,PName="Banana",Price=7.5},
15                 new Product{ID=4,PName="StrawBerry",Price=6.5},
16             };
17             //标准查询运算符
18             var query = arrStu.Join(arrPro, s => s.ID, p => p.ID, (s, p) =>new { s.SName, s.Age, p.PName, p.Price });
19             StringBuilder sbRes = new StringBuilder();
20             //打印
21             foreach (var item in query)
22             {
23                 sbRes.AppendFormat("SName:{0},Age:{1},PName:{2},Price:{3}", item.SName, item.Age, item.PName, item.Price);
24                 sbRes.AppendLine();
25             }
26             MessageBox.Show(sbRes.ToString());
27         }
View Code

  

原文地址:https://www.cnblogs.com/cherish836138981/p/6650527.html