码迷,mamicode.com
首页 > 其他好文 > 详细

LINQ学习——JOIN

时间:2017-03-31 11:50:40      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:for   query   作用   enum   不能   style   task   rod   click   

一、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

 

LINQ学习——JOIN

标签:for   query   作用   enum   不能   style   task   rod   click   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!