From cfd3a6f94db109061a3ecc4db36e958e6153223c Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Sun, 8 Sep 2019 21:52:28 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LinqToObject/AggregationTest.cs | 42 +++++++------- LinqStudy.Test/LinqToObject/ConcatTest.cs | 40 +++++++++++++ LinqStudy.Test/LinqToObject/SetsTest.cs | 58 ++++++++++++++++++- 3 files changed, 117 insertions(+), 23 deletions(-) create mode 100644 LinqStudy.Test/LinqToObject/ConcatTest.cs diff --git a/LinqStudy.Test/LinqToObject/AggregationTest.cs b/LinqStudy.Test/LinqToObject/AggregationTest.cs index 5087d29..a8b98f0 100644 --- a/LinqStudy.Test/LinqToObject/AggregationTest.cs +++ b/LinqStudy.Test/LinqToObject/AggregationTest.cs @@ -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 peoples = new List(); - 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(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 peoples = new List() { - + }; 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}, diff --git a/LinqStudy.Test/LinqToObject/ConcatTest.cs b/LinqStudy.Test/LinqToObject/ConcatTest.cs new file mode 100644 index 0000000..ed3cb75 --- /dev/null +++ b/LinqStudy.Test/LinqToObject/ConcatTest.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +using Xunit; +using LinqStudy; + +namespace LinqStudy.Test.LinqToObject +{ + /// + /// 串联操作 + /// + public class ConcatTest + { + /// + /// Concat:将两个序列合并成一个序列,不去重。与Union不同。 + /// + public void Concat_Test() + { + List peoples1 = new List() + { + new Person(){ Id=2, Name="小龙女", Age=28}, + new Person(){ Id=4, Name="杨过", Age=32}, + }; + + List peoples2 = new List() + { + 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); + } + } +} \ No newline at end of file diff --git a/LinqStudy.Test/LinqToObject/SetsTest.cs b/LinqStudy.Test/LinqToObject/SetsTest.cs index ec6fea3..4d22ab8 100644 --- a/LinqStudy.Test/LinqToObject/SetsTest.cs +++ b/LinqStudy.Test/LinqToObject/SetsTest.cs @@ -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 peoples1 = new List() { @@ -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(act); } #endregion + + #region Intersect + + /// + /// Intersect: 取交集 + /// + [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 peoples1 = new List() + { + p1,p2, + }; + + List peoples2 = new List() + { + p1,p3, + }; + + var intesectItem = peoples1.Intersect(peoples2); + var itemCount = intesectItem.Count(); + + Assert.Equal(1, itemCount); + } + + /// + /// 引用类型:使用 + /// + [Fact] + public void Intersect_Reference_Test() + { + List peoples1 = new List() + { + new Person(){ Id=1, Name="杨过", Age=32}, + new Person(){ Id=2, Name="小龙女", Age=28}, + + }; + + List peoples2 = new List() + { + new Person(){ Id=2, Name="小龙女", Age=28}, + new Person(){ Id=3, Name="云花公主", Age=16}, + }; + + //虽然两个“小龙女”属性相同,但不是一个对象,没有交集。 + var intesectItem = peoples1.Intersect(peoples2); + + Assert.Empty(intesectItem); + } + #endregion } } \ No newline at end of file