联接操作符

master
bicijinlian 6 years ago
parent 631f2cea14
commit 5ed8744464

@ -1,40 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using LinqStudy;
namespace LinqStudy.Test.LinqToObject
{
///<summary>
/// 串联操作
///<summary>
public class ConcatTest
{
///<summary>
/// Concat将两个序列合并成一个序列不去重。与Union不同。
///<summary>
public void Concat_Test()
{
List<Person> peoples1 = new List<Person>()
{
new Person(){ Id=2, Name="小龙女", Age=28},
new Person(){ Id=4, Name="杨过", Age=32},
};
List<Person> peoples2 = new List<Person>()
{
new Person(){ Id=2, Name="小龙女", Age=28},
new Person(){ Id=3, Name="云花公主", Age=16},
};
var concatAct = peoples1.Concat(peoples2);
var totalCount = concatAct.Count();
Assert.Equal(4, totalCount);
}
}
}

@ -0,0 +1,61 @@
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
}
}

@ -58,7 +58,7 @@ namespace LinqStudy.Test.LinqToObject
var lastName = oprationItem.Last().Name;
Assert.Equal("张三", firtName);
Assert.Equal("王五", lastName);
Assert.Equal("赵六", lastName);
}
/// <summary>

@ -381,5 +381,37 @@ namespace LinqStudy.Test.LinqToObject
Assert.Equal("杨过", iexcepItemName);
}
#endregion
#region Concat
///<summary>
/// 串联操作
///<summary>
public class ConcatTest
{
///<summary>
/// Concat将两个序列合并成一个序列不去重。与Union不同。
///<summary>
public void Concat_Test()
{
List<Person> peoples1 = new List<Person>()
{
new Person(){ Id=2, Name="小龙女", Age=28},
new Person(){ Id=4, Name="杨过", Age=32},
};
List<Person> peoples2 = new List<Person>()
{
new Person(){ Id=2, Name="小龙女", Age=28},
new Person(){ Id=3, Name="云花公主", Age=16},
};
var concatAct = peoples1.Concat(peoples2);
var totalCount = concatAct.Count();
Assert.Equal(4, totalCount);
}
}
#endregion
}
}
Loading…
Cancel
Save