diff --git a/LinqStudy.Test/LinqStudy.Test.csproj b/LinqStudy.Test/LinqStudy.Test.csproj index f70d100..86d69ce 100644 --- a/LinqStudy.Test/LinqStudy.Test.csproj +++ b/LinqStudy.Test/LinqStudy.Test.csproj @@ -20,10 +20,6 @@ - - - - diff --git a/LinqStudy.Test/LinqToObject/TypeConvertTest.cs b/LinqStudy.Test/LinqToObject/TypeConvertTest.cs index f875780..349e366 100644 --- a/LinqStudy.Test/LinqToObject/TypeConvertTest.cs +++ b/LinqStudy.Test/LinqToObject/TypeConvertTest.cs @@ -18,6 +18,7 @@ namespace LinqStudy.Test.LinqToObject /// public class TypeConvertTest { + #region AsEnumeralbe /// /// AsEnumeralbe操作符:将IEnumerable输入序列转换成IEnumerable的输出序列 /// 主要用于将一个实现了IEnumerable接口的对象转换成一个标准的IEnumerable接口对象。 @@ -33,16 +34,20 @@ namespace LinqStudy.Test.LinqToObject Assert.NotNull(students); } + #endregion + #region AsQueryable /// /// 主要应用与Linq to sql /// [Fact] public void AsQueryable_Test() { - // + // } + #endregion + #region OfType /// /// OfType:从序列中筛选指定类型的元素,并构建一个序列。 /// @@ -50,14 +55,16 @@ namespace LinqStudy.Test.LinqToObject public void OfType_Test() { //动态数组做为数据源 - ArrayList ints = new ArrayList() { 1,"2",3,"4",5}; + ArrayList ints = new ArrayList() { 1, "2", 3, "4", 5 }; var values = from i in ints.OfType() - select i; + select i; - Assert.Equal(new List() { 1,3,5}, values.ToList()); + Assert.Equal(new List() { 1, 3, 5 }, values.ToList()); } + #endregion + #region Cast /// /// Cast:将数据源中元素,强制转换成指定的类型。 /// 数据源null或存在不能转换的元素,则异常。 @@ -70,7 +77,7 @@ namespace LinqStudy.Test.LinqToObject var values = ints.Cast(); - Assert.Equal(new List() {"1","2","3","4","5"}, values.ToList()); + Assert.Equal(new List() { "1", "2", "3", "4", "5" }, values.ToList()); } [Fact] @@ -79,7 +86,7 @@ namespace LinqStudy.Test.LinqToObject //动态数组做为数据源 ArrayList ints = null; - Assert.Throws(()=> { + Assert.Throws(() => { ints.Cast(); }); } @@ -88,13 +95,15 @@ namespace LinqStudy.Test.LinqToObject public void Cast_InvalidCastException_Test() { //动态数组做为数据源 - ArrayList ints = new ArrayList() { 1,"2",3,"4"}; + ArrayList ints = new ArrayList() { 1, "2", 3, "4" }; Assert.Throws(() => { ints.Cast().ToList(); }); } + #endregion + #region ToList /// /// ToList:将IEnumerable转换为List, 立即执行。 /// 特别注意:此操作破坏"延迟执行",变为立即执行; @@ -103,7 +112,7 @@ namespace LinqStudy.Test.LinqToObject [Fact] public void ToList_Test() { - List email = new List() { "abc@163.com","bcd@126.com","111@163.com"}; + List email = new List() { "abc@163.com", "bcd@126.com", "111@163.com" }; var query = email.Where(q => q.Contains("@163.")).ToList(); @@ -145,43 +154,222 @@ namespace LinqStudy.Test.LinqToObject //元素对象不是同一个元素 Assert.NotSame(persons, person2); } + #endregion + #region ToDictionary + /// + /// ToDictionar转换字典,立即执行并保存快照。 + /// + [Fact] + public void ToDictionary_keySelector_Test() + { + List persons = new List() + { + new Person() {Id=1, Name="gaofeng",Age = 32 }, + new Person() {Id=2, Name="zhangsan",Age = 25 }, + }; + var dic = persons.ToDictionary(p => p.Id); - private DataTable GetDataTable() + Assert.Equal(1, dic[1].Id); + Assert.Equal(2, dic[2].Id); + } + + [Fact] + public void ToDictionary_keySelector_comparer_Test() + { + List persons = new List() + { + new Person() {Id=1, Name="gaofeng",Age = 32 }, + new Person() {Id=2, Name="zhangsan",Age = 25 }, + }; + + IEqualityComparer equal = new PersonEqual(); + + var dic = persons.ToDictionary(p => p.Id, EqualityComparer.Default); + + Assert.Equal("gaofeng", dic[1].Name); + Assert.Equal("zhangsan", dic[2].Name); + } + + [Fact] + public void ToDictionary_keySelector_elementSelector_Test() + { + List persons = new List() + { + new Person() {Id=1, Name="gaofeng",Age = 32 }, + new Person() {Id=2, Name="zhangsan",Age = 25 }, + }; + + var dic = persons.ToDictionary(p => p.Id, s => s.Name); + + Assert.Equal("gaofeng", dic[1]); + Assert.Equal("zhangsan", dic[2]); + } + + [Fact] + public void ToDictionary_keySelector_elementSelector_comparer_Test() + { + List persons = new List() + { + new Person() {Id=1, Name="gaofeng",Age = 32 }, + new Person() {Id=2, Name="zhangsan",Age = 25 }, + }; + + var dic = persons.ToDictionary(p => p.Id, s => s.Name, EqualityComparer.Default); + + Assert.Equal("gaofeng", dic[1]); + Assert.Equal("zhangsan", dic[2]); + } + + [Fact] + public void ToDictionary_ArgumentException_Test() + { + List persons = new List() + { + new Person() {Id=1, Name="gaofeng",Age = 32 }, + new Person() {Id=1, Name="zhangsan",Age = 25 }, + }; + + Assert.Throws(() => { + persons.ToDictionary(p => p.Id, s => s.Name); + }); + } + + [Fact] + public void ToDictionary_ArgumentNullException_Test() + { + List persons = new List() + { + new Person() {Id=1, Name=null,Age = 32 }, + new Person() {Id=1, Name="zhangsan",Age = 25 }, + }; + + Assert.Throws(() => { + persons.ToDictionary(p => p.Name, s => s.Name); + }); + } + + [Fact] + public void ToDictionary_DataSource_NullException_Test() + { + List persons = null; + + Assert.Throws(() => { + persons.ToDictionary(p => p.Name, s => s.Name); + }); + } + #endregion + + #region ToLookup + + /// + /// ToLookup:根据指定的键选择器函数从IEnumerable创建Lookup + /// Lookup对象表示一个一对多的字典,定义了一个由键和序列组成的元组,类似GroupJoin返回的结果。 + /// + [Fact] + public void ToLookup_Test() + { + var persons = new List() + { + new Employee(){ Id=1, Name="gaofeng",Emails=new List(){"a1@163.com","a2@163.com","a3@163.com"} }, + new Employee(){ Id=1, Name="zhangsan",Emails=new List(){ "b1@163.com", "b2@163.com", "b3@163.com" } }, + + new Employee(){ Id=2, Name="gaofeng2",Emails=new List(){"c1@163.com","c2@163.com","c3@163.com"} }, + new Employee(){ Id=2, Name="zhangsan2",Emails=new List(){ "d1@163.com", "d2@163.com", "d3@163.com" } }, + }; + + var lookup = persons.ToLookup(q => q.Id); + var group2 = lookup[2]; + var group2_count = group2.Count(); + + Assert.Equal(2,group2_count); + } + + [Fact] + public void ToLookup_keySelector_comparer_Test() { + var persons = new List() + { + new Employee(){ Id=1, Name="gaofeng",Emails=new List(){"a1@163.com","a2@163.com","a3@163.com"} }, + new Employee(){ Id=1, Name="zhangsan",Emails=new List(){ "b1@163.com", "b2@163.com", "b3@163.com" } } + }; - DataTable dt=new DataTable(); - DataColumn col_id=new DataColumn("Id"); - col_id.AllowDBNull=false; - col_id.AutoIncrementSeed=1; - col_id.AutoIncrement=true; - col_id.AutoIncrementStep=1; - col_id.DataType=typeof(int); + var lookup = persons.ToLookup(q => q.Id,EqualityComparer.Default); + var group1_firstEmail = lookup[1].FirstOrDefault().Emails.FirstOrDefault() ; - DataColumn col_name=new DataColumn("Name"); - col_name.AllowDBNull=true; - col_name.AutoIncrement=false; - col_name.DataType=typeof(string); - col_name.DefaultValue=string.Empty; + Assert.Equal("a1@163.com", group1_firstEmail); + } + + [Fact] + public void ToLookup_keySelector_elementSelector_Test() + { + var persons = new List() + { + new Employee(){ Id=1, Name="gaofeng",Emails=new List(){"a1@163.com","a2@163.com","a3@163.com"} }, + new Employee(){ Id=1, Name="zhangsan",Emails=new List(){ "b1@163.com", "b2@163.com", "b3@163.com" } } + }; + + var lookup = persons.ToLookup(q => q.Id, e=>e.Emails); + var group1_Emails = lookup[1].FirstOrDefault(); + var group1_Emails_first = group1_Emails.FirstOrDefault(); + + Assert.Equal("a1@163.com", group1_Emails_first); + } + + [Fact] + public void ToLookup_keySelector_elementSelector_comparer_Test() + { + var persons = new List() + { + new Employee(){ Id=1, Name="gaofeng",Emails=new List(){"a1@163.com","a2@163.com","a3@163.com"} }, + new Employee(){ Id=1, Name="zhangsan",Emails=new List(){ "b1@163.com", "b2@163.com", "b3@163.com" } } + }; + + var lookup = persons.ToLookup(q => q.Name, e => e.Emails,EqualityComparer.Default); + var group1_Emails = lookup["gaofeng"].FirstOrDefault(); + var group1_Emails_first = group1_Emails.FirstOrDefault(); - DataColumn col_age=new DataColumn("Age"); - col_age.AllowDBNull=false; - col_age.AutoIncrement=false; - col_age.DataType=typeof(int); - col_age.DefaultValue=20; + Assert.Equal("a1@163.com", group1_Emails_first); + } + #endregion + + #region private + + private DataTable GetDataTable() + { + + DataTable dt = new DataTable(); + DataColumn col_id = new DataColumn("Id"); + col_id.AllowDBNull = false; + col_id.AutoIncrementSeed = 1; + col_id.AutoIncrement = true; + col_id.AutoIncrementStep = 1; + col_id.DataType = typeof(int); + + DataColumn col_name = new DataColumn("Name"); + col_name.AllowDBNull = true; + col_name.AutoIncrement = false; + col_name.DataType = typeof(string); + col_name.DefaultValue = string.Empty; + + DataColumn col_age = new DataColumn("Age"); + col_age.AllowDBNull = false; + col_age.AutoIncrement = false; + col_age.DataType = typeof(int); + col_age.DefaultValue = 20; dt.Columns.Add(col_id); dt.Columns.Add(col_name); dt.Columns.Add(col_age); var row_one = dt.NewRow(); - row_one[1]="王高峰"; - row_one[2]=21; + row_one[1] = "王高峰"; + row_one[2] = 21; var row_2 = dt.NewRow(); - row_2[1]="王向前"; - row_2[2]=32; + row_2[1] = "王向前"; + row_2[2] = 32; dt.Rows.Add(row_one); dt.Rows.Add(row_2); @@ -209,6 +397,6 @@ namespace LinqStudy.Test.LinqToObject return arrStudent; } - + #endregion } }