diff --git a/LinqStudy.Test/LinqToObject/CreateTest.cs b/LinqStudy.Test/LinqToObject/CreateTest.cs index cdcc4d1..f8ea03b 100644 --- a/LinqStudy.Test/LinqToObject/CreateTest.cs +++ b/LinqStudy.Test/LinqToObject/CreateTest.cs @@ -9,10 +9,11 @@ using FluentAssertions; namespace LinqStudy.Test.LinqToObject { /// - /// 生成操作符 + /// 生成操作符 /// public class CreateTest { + #region Empty /// /// Empty 返回指定类型的空集,没有任何元素的集合。 /// Enumerable的静态方法,常用来做初始种子集合。 @@ -20,10 +21,10 @@ namespace LinqStudy.Test.LinqToObject [Fact] public void Empty_string_Test() { - var ini= Enumerable.Empty(); + var ini = Enumerable.Empty(); - Assert.IsType(ini); - Assert.Empty(ini); + Assert.IsType(ini); + Assert.Empty(ini); } /// @@ -37,6 +38,9 @@ namespace LinqStudy.Test.LinqToObject Assert.IsType(ini); Assert.Empty(ini); } + #endregion + + #region DefaultIfEmpty /// /// DefaultIfEmpty @@ -45,11 +49,12 @@ namespace LinqStudy.Test.LinqToObject [Fact] public void DefaultIfEmpty_Test() { - var defaultValue= Enumerable.DefaultIfEmpty(Enumerable.Empty()); + var defaultValue = Enumerable.DefaultIfEmpty(Enumerable.Empty()); Assert.Single(defaultValue); Assert.Equal(default(int), defaultValue.First()); } + /// /// 数据源为Null时,抛出异常。 /// @@ -61,6 +66,9 @@ namespace LinqStudy.Test.LinqToObject Assert.Single(defaultValue); Assert.Equal(default(Person), defaultValue.First()); } + #endregion + + #region Range /// /// Range:生成指定范围的整数序列 @@ -86,7 +94,8 @@ namespace LinqStudy.Test.LinqToObject //生成100项值均为"test@163.com"的字符串序列 var values = Enumerable.Repeat("test@163.com", 100); - Array.ForEach(values.ToArray(), (emai) => { + Array.ForEach(values.ToArray(), (emai) => + { Assert.Equal("test@163.com", emai); }); } @@ -98,8 +107,8 @@ namespace LinqStudy.Test.LinqToObject public void Repeat_TestData_Test() { //生成测试数据:生成了有100项的一个序列,这100项均是对同一个对象的引用,而不是100个对象。 - Random rdm = new Random(DateTime.Now.Ticks.GetHashCode()); + //虽然生成时,用了随机数,但是因对象只生成了一次,其它均是这个对象的浅拷贝,所以100个对象,完全相同:100个对同一对象的引用。 var persons = Enumerable.Repeat(new Person() { @@ -111,23 +120,26 @@ namespace LinqStudy.Test.LinqToObject Assert.Equal(100, persons.Count); //所有对象引用相同 - Assert.All(persons, (p) => + Assert.All(persons, (p) => { Assert.Same(persons[0], p); }); } + #endregion + + #region SequenceEqual + /// /// SequenceEqual: 比较源和目标序列,返回一个bool值,指示所含元素是否相同。 /// [Fact] public void SequenceEqual_Test() { - Person p=new Person(){Id=1,Name="王小明",Age=20}; - - var s1=Enumerable.Repeat(p,100); + Person p = new Person() { Id = 1, Name = "王小明", Age = 20 }; - var s2=Enumerable.Repeat(p,100); + var s1 = Enumerable.Repeat(p, 100); + var s2 = Enumerable.Repeat(p, 100); Assert.True(s1.SequenceEqual(s2)); } @@ -135,13 +147,14 @@ namespace LinqStudy.Test.LinqToObject [Fact] public void SequenceEqual_No_Test() { - Person p=new Person(){Id=1,Name="王小明",Age=20}; + Person p = new Person() { Id = 1, Name = "王小明", Age = 20 }; - var s1=Enumerable.Repeat(p,100); + var s1 = Enumerable.Repeat(p, 100); - var s2=Enumerable.Repeat(p,200); + var s2 = Enumerable.Repeat(p, 200); Assert.False(s1.SequenceEqual(s2)); } + #endregion } } diff --git a/LinqStudy.Test/LinqToObject/GroupTest.cs b/LinqStudy.Test/LinqToObject/GroupTest.cs index 2da2993..9fd3943 100644 --- a/LinqStudy.Test/LinqToObject/GroupTest.cs +++ b/LinqStudy.Test/LinqToObject/GroupTest.cs @@ -39,7 +39,7 @@ namespace LinqStudy.Test.LinqToObject } /// - /// 延迟执行 + /// 延迟执行 /// [Fact] public void GroupBy_Delay_Test() @@ -128,8 +128,9 @@ namespace LinqStudy.Test.LinqToObject Assert.Equal(expected,actual); } + /// - /// 重载4:keySelector elementSelector comparer + /// 重载4:keySelector elementSelector comparer /// [Fact] public void GroupBy_keySelector_elementSelector_comparer_Test() diff --git a/LinqStudy.Test/LinqToObject/ProjectiveTest.cs b/LinqStudy.Test/LinqToObject/ProjectiveTest.cs index 8dca176..696c4e6 100644 --- a/LinqStudy.Test/LinqToObject/ProjectiveTest.cs +++ b/LinqStudy.Test/LinqToObject/ProjectiveTest.cs @@ -16,6 +16,8 @@ namespace LinqStudy.Test.LinqToObject /// public class ProjectiveTest { + #region Select + /// /// Select投影:简单投影,一对一 /// @@ -26,11 +28,12 @@ namespace LinqStudy.Test.LinqToObject var persons = PersonManager.GetPersons(); // Act - var maps = persons.Select(p=>p.Age).ToList(); - + var maps = persons.Select(p => p.Age).ToList(); + // Assert Assert.IsType>(maps); } + /// /// 投影为匿名类 /// @@ -41,8 +44,8 @@ namespace LinqStudy.Test.LinqToObject var persons = PersonManager.GetPersons(); // Act - var maps = persons.Select(p=>new {Id=p.Id, Node=$"姓名{p.Name},年龄{p.Age}."}).ToList(); - + var maps = persons.Select(p => new { Id = p.Id, Node = $"姓名{p.Name},年龄{p.Age}." }).ToList(); + // Assert Assert.IsNotType>(maps); } @@ -55,12 +58,15 @@ namespace LinqStudy.Test.LinqToObject { var persons = PersonManager.GetPersons(); - var maps = persons.Select((query,index)=> new KeyValuePair(index,query)).ToList(); + var maps = persons.Select((query, index) => new KeyValuePair(index, query)).ToList(); var indexs = persons.Select((query, index) => index).ToList(); Assert.IsType>>(maps); - Assert.Equal(0,indexs[0]); + Assert.Equal(0, indexs[0]); } + #endregion + + #region SelectMany /// /// 将序列的每个元素投影到 IEnumerable 并将结果序列合并为一个序列。 @@ -82,7 +88,7 @@ namespace LinqStudy.Test.LinqToObject new Employee(){Id=5,Name="东升",Emails=new List(){ "ebc@163.com", "ecd@163.com", "ede@163.com" }}, }; - var maps = employees.SelectMany(q=>q.Emails).ToList(); + var maps = employees.SelectMany(q => q.Emails).ToList(); Assert.IsType>(maps); Assert.Equal(15, maps.Count); @@ -106,7 +112,7 @@ namespace LinqStudy.Test.LinqToObject new Employee(){Id=5,Name="东升",Emails=new List(){ "ebc@163.com", "ecd@163.com", "ede@163.com" }}, }; - var maps = employees.SelectMany((q, idx) => + var maps = employees.SelectMany((q, idx) => { q.Emails.Add(idx.ToString()); return q.Emails; @@ -134,7 +140,7 @@ namespace LinqStudy.Test.LinqToObject new Employee(){ Id=5,Name="东升",Emails=new List(){ "e1@163.com", "e2@163.com", "e3@163.com" } }, }; - var maps = employees.SelectMany((employee)=> employee.Emails,(person,email)=> new { Name=person.Name,Email=email}).ToList(); + var maps = employees.SelectMany((employee) => employee.Emails, (person, email) => new { Name = person.Name, Email = email }).ToList(); Assert.Equal(15, maps.Count); } @@ -160,12 +166,12 @@ namespace LinqStudy.Test.LinqToObject var maps = employees.SelectMany ( - (employee, idx) => + (employee, idx) => { return new List() { new Employee() { Id = employee.Id, Name = employee.Name + "_" + idx, Emails = employee.Emails } }; - }, + }, - (person,email) => new + (person, email) => new { Name = email.Name, Email = email.Emails @@ -175,26 +181,6 @@ namespace LinqStudy.Test.LinqToObject Assert.Equal(5, maps.Count); } - - [Fact] - public void Test() - { - var cc= DocDemo(""); - } - - /// - /// Doc示例 - /// - /// 参数 - /// - /// ArgumentNullException - /// - /// DecDemo("dsfsadf") - /// - /// 参数+“-” - private string DocDemo(string aa) - { - return aa + "-"; - } + #endregion } } diff --git a/LinqStudy.Test/LinqToObject/SequenceEqualTest.cs b/LinqStudy.Test/LinqToObject/SequenceEqualTest.cs index 79dd73d..9a3b75f 100644 --- a/LinqStudy.Test/LinqToObject/SequenceEqualTest.cs +++ b/LinqStudy.Test/LinqToObject/SequenceEqualTest.cs @@ -14,6 +14,7 @@ namespace LinqStudy.Test.LinqToObject /// public class SequenceEqualTest { + #region SequenceEqual /// /// SequenceEqual /// 序列相等比较:所含项数量相同、内容相同、位置相同、引用类型则引用相同的对象,则两个序列相等。 @@ -60,7 +61,6 @@ namespace LinqStudy.Test.LinqToObject [Fact] public void SequenceEqual_ValueType_Test() { - List sequence1 = new List() { 1, 2, 3 }; List sequence2 = new List() { 1, 2, 3 }; @@ -194,5 +194,6 @@ namespace LinqStudy.Test.LinqToObject Assert.True(sequence1.SequenceEqual(sequence2, new PersonEqualityComparer())); } + #endregion } } diff --git a/LinqStudy.Test/LinqToObject/SetsTest.cs b/LinqStudy.Test/LinqToObject/SetsTest.cs index 12d8ff8..ced4bd1 100644 --- a/LinqStudy.Test/LinqToObject/SetsTest.cs +++ b/LinqStudy.Test/LinqToObject/SetsTest.cs @@ -7,8 +7,8 @@ using Xunit; namespace LinqStudy.Test.LinqToObject { /// - /// Sets 集合操作符 - /// 注意:集合操作为 “延迟执行”,数据源为null时均引发异常。 + /// Sets 集合操作符 + /// 注意:集合操作为 “延迟执行”,数据源为null时均引发异常。 /// public class SetsTest { diff --git a/LinqStudy.Test/LinqToObject/WhereTest.cs b/LinqStudy.Test/LinqToObject/WhereTest.cs index fc54e67..9d5661b 100644 --- a/LinqStudy.Test/LinqToObject/WhereTest.cs +++ b/LinqStudy.Test/LinqToObject/WhereTest.cs @@ -13,6 +13,8 @@ namespace LinqStudy.Test.LinqToObject /// public class WhereTest { + #region Where + /// /// where:过滤查询条件 /// @@ -39,9 +41,10 @@ namespace LinqStudy.Test.LinqToObject { List Persons = null; - Action act = ()=> { Persons.Where(p => p.Name.StartsWith("zhang")); }; + Action act = () => { Persons.Where(p => p.Name.StartsWith("zhang")); }; Assert.Throws(act); } + #endregion } } diff --git a/LinqStudy.Test/Other/IndexersTest.cs b/LinqStudy.Test/Other/IndexersTest.cs index 42e6e0b..6fdf5b4 100644 --- a/LinqStudy.Test/Other/IndexersTest.cs +++ b/LinqStudy.Test/Other/IndexersTest.cs @@ -76,6 +76,5 @@ namespace LinqStudy.Test.Other Employees[idx] = value; } } - } }