|
|
|
@ -275,6 +275,111 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
|
|
|
|
|
|
Assert.Empty(intesectItem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///<summary>
|
|
|
|
|
/// 重载:自定义比较器 comparer
|
|
|
|
|
///</sumaary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Intersect_comparer_Test()
|
|
|
|
|
{
|
|
|
|
|
List<Person> peoples1 = new List<Person>()
|
|
|
|
|
{
|
|
|
|
|
new Person(){ Id=1, Name="杨过", Age=32},
|
|
|
|
|
new Person(){ Id=2, Name="小龙女", Age=28},
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
List<Person> peoples2 = new List<Person>()
|
|
|
|
|
{
|
|
|
|
|
new Person(){ Id=2, Name="小龙女", Age=28},
|
|
|
|
|
new Person(){ Id=3, Name="云花公主", Age=16},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//虽然两个“小龙女”属性相同,但不是一个对象,没有交集。
|
|
|
|
|
var intesectItem = peoples1.Intersect(peoples2, new PersonEqualityComparerById());
|
|
|
|
|
var intesctItemName = intesectItem.First().Name;
|
|
|
|
|
|
|
|
|
|
Assert.Equal("小龙女", intesctItemName);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Intersect
|
|
|
|
|
|
|
|
|
|
///<summary>
|
|
|
|
|
/// Except: 从一个序列中排除另一个序列中的项。
|
|
|
|
|
///</summary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Except_Test()
|
|
|
|
|
{
|
|
|
|
|
var p1 = new Person() { Id = 1, Name = "杨过", Age = 32 };
|
|
|
|
|
var p2 = new Person() { Id = 2, Name = "小龙女", Age = 28 };
|
|
|
|
|
var p3 = new Person() { Id = 3, Name = "云花公主", Age = 16 };
|
|
|
|
|
|
|
|
|
|
List<Person> peoples1 = new List<Person>()
|
|
|
|
|
{
|
|
|
|
|
p1,p2,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
List<Person> peoples2 = new List<Person>()
|
|
|
|
|
{
|
|
|
|
|
p1,p3,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var exceptItem = peoples1.Except(peoples2);
|
|
|
|
|
var itemName = exceptItem.First().Name;
|
|
|
|
|
|
|
|
|
|
Assert.Equal("小龙女", itemName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///<summary>
|
|
|
|
|
/// 引用类型:使用
|
|
|
|
|
///</sumaary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Except_Reference_Test()
|
|
|
|
|
{
|
|
|
|
|
List<Person> peoples1 = new List<Person>()
|
|
|
|
|
{
|
|
|
|
|
new Person(){ Id=1, Name="杨过", Age=32},
|
|
|
|
|
new Person(){ Id=2, Name="小龙女", Age=28},
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
List<Person> peoples2 = new List<Person>()
|
|
|
|
|
{
|
|
|
|
|
new Person(){ Id=2, Name="小龙女", Age=28},
|
|
|
|
|
new Person(){ Id=3, Name="云花公主", Age=16},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var exceptItem = peoples1.Except(peoples2);
|
|
|
|
|
var itemCount = exceptItem.Count();
|
|
|
|
|
|
|
|
|
|
Assert.Equal(2, itemCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///<summary>
|
|
|
|
|
/// 重载:自定义比较器 comparer
|
|
|
|
|
///</sumaary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Except_comparer_Test()
|
|
|
|
|
{
|
|
|
|
|
List<Person> peoples1 = new List<Person>()
|
|
|
|
|
{
|
|
|
|
|
new Person(){ Id=1, Name="杨过", Age=32},
|
|
|
|
|
new Person(){ Id=2, Name="小龙女", Age=28},
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
List<Person> peoples2 = new List<Person>()
|
|
|
|
|
{
|
|
|
|
|
new Person(){ Id=2, Name="小龙女", Age=28},
|
|
|
|
|
new Person(){ Id=3, Name="云花公主", Age=16},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var exceptItem = peoples1.Except(peoples2, new PersonEqualityComparerById());
|
|
|
|
|
var iexcepItemName = exceptItem.First().Name;
|
|
|
|
|
|
|
|
|
|
Assert.Equal("杨过", iexcepItemName);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|