master
bicijinlian 6 years ago
parent 33880782c7
commit cfd3a6f94d

@ -26,7 +26,7 @@ namespace LinqStudy.Test.LinqToObject
{
new Person(){ Id=1, Name="张三丰", Age=230},
new Person(){ Id=2, Name="小龙女", Age=28},
new Person(){ Id=3, Name="花公主", Age=16},
new Person(){ Id=3, Name="花公主", Age=16},
new Person(){ Id=4, Name="杨过", Age=32},
//new Person(){ Id=5, Name="东方不败", Age=86},
//new Person(){ Id=6, Name="金轮法王", Age=64},
@ -43,7 +43,7 @@ namespace LinqStudy.Test.LinqToObject
// 由于第4项是最后一项整个“聚合操作”结束返回本次“聚合项”作为最后的输出。
var agg = peoples.Aggregate
(
(aggregateItem, next) =>
(aggregateItem, next) =>
{
//把当前元素的年龄加到返回的“聚合项”的年龄,
//最后的聚合项的年龄就是“总年龄”。
@ -70,11 +70,11 @@ namespace LinqStudy.Test.LinqToObject
{
new Person(){ Id=1, Name="张三丰", Age=230},
new Person(){ Id=2, Name="小龙女", Age=28},
new Person(){ Id=3, Name="花公主", Age=16},
new Person(){ Id=3, Name="花公主", Age=16},
new Person(){ Id=4, Name="杨过", Age=32},
};
var seed =0;
var seed = 0;
//因为指定了初始聚合项一共执行了4次“聚合操作”即每一项执行一次“聚合操作”
var agg = peoples.Aggregate
@ -82,9 +82,9 @@ namespace LinqStudy.Test.LinqToObject
seed,
(accumulate, currentItem) =>
{
return accumulate + currentItem.Age;
return accumulate + currentItem.Age;
}
) ;
);
var totalAge = agg;
var expectedAge = 230 + 28 + 16 + 32;
@ -102,7 +102,7 @@ namespace LinqStudy.Test.LinqToObject
{
new Person(){ Id=1, Name="张三丰", Age=230},
new Person(){ Id=2, Name="小龙女", Age=28},
new Person(){ Id=3, Name="花公主", Age=16},
new Person(){ Id=3, Name="花公主", Age=16},
new Person(){ Id=4, Name="杨过", Age=32},
};
@ -121,11 +121,11 @@ namespace LinqStudy.Test.LinqToObject
},
//改变最后聚合结果,并返回。
accumulateItem => "总年龄为:"+ accumulateItem.ToString()
accumulateItem => "总年龄为:" + accumulateItem.ToString()
);
var totalAge = agg;
var expectedAge = "总年龄为:"+ (230 + 28 + 16 + 32).ToString();
var expectedAge = "总年龄为:" + (230 + 28 + 16 + 32).ToString();
Assert.Equal(expectedAge.ToString(), totalAge);
}
@ -138,14 +138,14 @@ namespace LinqStudy.Test.LinqToObject
{
List<Person> peoples = new List<Person>();
Action act =() => peoples.Aggregate
(
(aggregateItem, next) =>
{
next.Age = next.Age + aggregateItem.Age;
return next;
}
);
Action act = () => peoples.Aggregate
(
(aggregateItem, next) =>
{
next.Age = next.Age + aggregateItem.Age;
return next;
}
);
Assert.Throws<InvalidOperationException>(act);
}
@ -204,7 +204,7 @@ namespace LinqStudy.Test.LinqToObject
new Person(){ Id=4, Name="杨过", Age=32},
};
var count = peoples.Count(p=>p.Age>28);
var count = peoples.Count(p => p.Age > 28);
var expected = 1;
Assert.Equal(count, expected);
@ -262,7 +262,7 @@ namespace LinqStudy.Test.LinqToObject
{
new Person(){ Id=1, Name="张三丰", Age=230},
new Person(){ Id=2, Name="小龙女", Age=28},
new Person(){ Id=3, Name="花公主", Age=16},
new Person(){ Id=3, Name="花公主", Age=16},
new Person(){ Id=4, Name="杨过", Age=32},
new Person(){ Id=5, Name="东方不败", Age=86},
new Person(){ Id=6, Name="金轮法王", Age=64},
@ -286,7 +286,7 @@ namespace LinqStudy.Test.LinqToObject
{
List<Person> peoples = new List<Person>()
{
};
Action act = () => peoples.Max(p => p.Age);
@ -307,7 +307,7 @@ namespace LinqStudy.Test.LinqToObject
{
new Person(){ Id=1, Name="张三丰", Age=230},
new Person(){ Id=2, Name="小龙女", Age=28},
new Person(){ Id=3, Name="花公主", Age=16},
new Person(){ Id=3, Name="花公主", Age=16},
new Person(){ Id=4, Name="杨过", Age=32},
new Person(){ Id=5, Name="东方不败", Age=86},
new Person(){ Id=6, Name="金轮法王", Age=64},

@ -0,0 +1,40 @@
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);
}
}
}

@ -122,7 +122,7 @@ namespace LinqStudy.Test.LinqToObject
{
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 };
var p3 = new Person() { Id = 3, Name = "花公主", Age = 16 };
List<Person> peoples1 = new List<Person>()
{
@ -155,7 +155,7 @@ namespace LinqStudy.Test.LinqToObject
{
new Person(){ Id=1, Name="张三丰", Age=230},
new Person(){ Id=2, Name="小龙女", Age=28},
new Person(){ Id=3, Name="花公主", Age=16},
new Person(){ Id=3, Name="花公主", Age=16},
new Person(){ Id=4, Name="杨过", Age=32},
};
@ -222,5 +222,59 @@ namespace LinqStudy.Test.LinqToObject
Assert.Throws<ArgumentNullException>(act);
}
#endregion
#region Intersect
///<summary>
/// Intersect: 取交集
///</summary>
[Fact]
public void Intersect_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 intesectItem = peoples1.Intersect(peoples2);
var itemCount = intesectItem.Count();
Assert.Equal(1, itemCount);
}
///<summary>
/// 引用类型:使用
///</sumaary>
[Fact]
public void Intersect_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 intesectItem = peoples1.Intersect(peoples2);
Assert.Empty(intesectItem);
}
#endregion
}
}
Loading…
Cancel
Save