一、JOIN的作用

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

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

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

二、LINQ表达式的语法

Jion Identifier in Collection2 On Field1 equals Field2

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

示例:Student.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LINQDemoWinForm
{
class Student
{
public int ID { get;set;}
public string SName { get; set; }
public int Age { get; set; }
}
}

Product.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LINQDemoWinForm
{
class Product
{
public int ID { get; set; }
public string PName { get; set; }
public double Price { get; set; }
}
}

LINQ语句

 private void button1_Click(object sender, EventArgs e)
{
//初始化Student数组
Student[] arrStu = new Student[]{
new Student{ID=,SName="zhangsan",Age=},
new Student{ID=,SName="lisi",Age=},
new Student{ID=,SName="wangwu",Age=},
new Student{ID=,SName="liuliu",Age=},
};
//初始化Product数组
Product[] arrPro = new Product[]{
new Product{ID=,PName="Apple",Price=2.25},
new Product{ID=,PName="Orange",Price=5.25},
new Product{ID=,PName="Banana",Price=7.5},
new Product{ID=,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());
}

执行结果:

LINQ学习——JOIN-LMLPHP

三、标准查询运算符—Join

原函数:

  //
// 摘要:
// 基于匹配键对两个序列的元素进行关联。 使用默认的相等比较器对键进行比较。
//
// 参数:
// outer:
// 要联接的第一个序列。
//
// inner:
// 要与第一个序列联接的序列。
//
// outerKeySelector:
// 用于从第一个序列的每个元素提取联接键的函数。
//
// innerKeySelector:
// 用于从第二个序列的每个元素提取联接键的函数。
//
// resultSelector:
// 用于从两个匹配元素创建结果元素的函数。
//
// 类型参数:
// TOuter:
// 第一个序列中的元素的类型。
//
// TInner:
// 第二个序列中的元素的类型。
//
// TKey:
// 键选择器函数返回的键的类型。
//
// TResult:
// 结果元素的类型。
//
// 返回结果:
// 一个具有 TResult 类型元素的 System.Collections.Generic.IEnumerable<T>,这些元素是通过对两个序列执行内部联接得来的。
//
// 异常:
// System.ArgumentNullException:
// outer 或 inner 或 outerKeySelector 或 innerKeySelector 或 resultSelector 为 null。
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);

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

示例:

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

 private void button1_Click_1(object sender, EventArgs e)
{
//初始化Student数组
Student[] arrStu = new Student[]{
new Student{ID=,SName="zhangsan",Age=},
new Student{ID=,SName="lisi",Age=},
new Student{ID=,SName="wangwu",Age=},
new Student{ID=,SName="liuliu",Age=},
};
//初始化Product数组
Product[] arrPro = new Product[]{
new Product{ID=,PName="Apple",Price=2.25},
new Product{ID=,PName="Orange",Price=5.25},
new Product{ID=,PName="Banana",Price=7.5},
new Product{ID=,PName="StrawBerry",Price=6.5},
};
//标准查询运算符
var query = arrStu.Join(arrPro, s => s.ID, p => p.ID, (s, p) =>new { s.SName, s.Age, p.PName, p.Price });
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());
}
05-17 12:38