LinqStudy/LinqStudy.Test/LinqToObject/JoinTest.cs

61 lines
1.8 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using LinqStudy;
namespace LinqStudy.Test.LinqToObject
{
///<summary>
/// 联接操作:将一个数据源对象与另一个数据源对象进行关联或联合的操作。
/// 延迟执行
/// 数据源为null,均引发异常
///<summary>
public class JoinTest
{
#region Jion
/// <summary>
/// Jion将一个数据源与另一个数据源相联接根据两个数据源中相等的值进行匹配
/// 类似T-SQL中的Inner Join
/// 延迟执行
/// </summary>
[Fact]
public void Jion_Test()
{
List<Person> peoples = new List<Person>()
{
new Person(){ Id=1, Name="小龙女",Age=18 },
new Person(){ Id=2, Name="郭靖",Age=28 },
};
List<Teacher> teachers = new List<Teacher>()
{
new Teacher(){ Id=1, Name="杨过老师",Hometown=new Hometown(){ Province="终南山",City="古墓"} },
new Teacher(){ Id=2, Name="郭老师",Hometown=new Hometown(){ Province="湖北省",City="襄阳"} },
new Teacher(){ Id=3, Name="金轮法师",Hometown=new Hometown(){ Province="西藏",City="游民"} },
};
var jionItem = peoples.Join
(
teachers, p => p.Id,
t => t.Id,
(p, t) => new
{
Id = 1,
Name = p.Name + "-" + t.Name,
Age = 18,
Hometown = t.Hometown
}
)
.ToList();
Assert.Equal(2, jionItem.Count);
}
#endregion
#region GroupJion
#endregion
}
}