diff --git a/LinqStudy.Test/LinqToObject/BaseTest.cs b/LinqStudy.Test/LinqToObject/BaseTest.cs index 18b8b3d..cecec15 100644 --- a/LinqStudy.Test/LinqToObject/BaseTest.cs +++ b/LinqStudy.Test/LinqToObject/BaseTest.cs @@ -10,7 +10,9 @@ using Xunit; namespace LinqStudy.Test.LinqToObject { /// - /// 基本项测试 + /// 查询运算符:本质定义在下面程序集中的扩展方法,少量静态方法。 + /// System.Linq.Enumerable + /// System.Linq.Queryable /// public class BaseTest { diff --git a/LinqStudy.Test/LinqToObject/ConvertTest.cs b/LinqStudy.Test/LinqToObject/ConvertTest.cs new file mode 100644 index 0000000..1ad9e06 --- /dev/null +++ b/LinqStudy.Test/LinqToObject/ConvertTest.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using Xunit; + +namespace LinqStudy.Test.LinqToObject +{ + /// + /// 转换运算符 + /// + public class ConvertTest + { + [Fact] + public void Test() + { + var cc= Enumerable.Empty(); + } + } +} diff --git a/LinqStudy.Test/LinqToObject/CreateTest.cs b/LinqStudy.Test/LinqToObject/CreateTest.cs index 0aaacc5..407a8f0 100644 --- a/LinqStudy.Test/LinqToObject/CreateTest.cs +++ b/LinqStudy.Test/LinqToObject/CreateTest.cs @@ -1,4 +1,10 @@ using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +using Xunit; +using FluentAssertions; namespace LinqStudy.Test.LinqToObject { @@ -7,5 +13,32 @@ namespace LinqStudy.Test.LinqToObject /// public class CreateTest { + /// + /// Empty 返回指定类型的空集,没有任何元素的集合。 + /// Enumerable的静态方法,常用来做初始种子集合。 + /// + /// + /// Enumerable + /// + [Fact] + public void Empty_string_Test() + { + var ini= Enumerable.Empty(); + + Assert.IsType(ini); + Assert.Empty(ini); + } + + /// + /// 注意:返回值为Enumerable,而不是List + /// + [Fact] + public void Empty_Class_Test() + { + var ini = Enumerable.Empty(); + + Assert.IsType(ini); + Assert.Empty(ini); + } } } diff --git a/LinqStudy.Test/Other/IndexersTest.cs b/LinqStudy.Test/Other/IndexersTest.cs new file mode 100644 index 0000000..42e6e0b --- /dev/null +++ b/LinqStudy.Test/Other/IndexersTest.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Text; + +using Xunit; + +namespace LinqStudy.Test.Other +{ + /// + /// 索引器测试 + /// + public class IndexersTest + { + [Fact] + public void Test() + { + PersonIndexers indexer = new PersonIndexers( PersonManager.GetPersons()); + + Assert.Equal(1, indexer[0].Id); + } + } + + public class PersonIndexers + { + private List Persons=new List(); + + public PersonIndexers(List people) + { + Persons = people; + } + + public Person this[int index] + { + get + { + if (index >= 0 && index < Persons.Count) + { + return Persons[index]; + } + else + { + return null; + } + } + set { + if (index >= 0 && index < Persons.Count) + { + Persons[index] = value; + } + else + { + throw new IndexOutOfRangeException(); + } + } + } + } + + public class EmployeeIndexers + { + private List Employees; + + public EmployeeIndexers(List _employees) + { + Employees = _employees; + } + + public Employee this[string name] + { + get + { + return Employees.Find(q => q.Name== name); + } + set + { + var idx= Employees.FindIndex(q => q.Id == value.Id); + Employees[idx] = value; + } + } + + } +} diff --git a/LinqStudy/IEnumerableDemo/Demo.cs b/LinqStudy/IEnumerableDemo/Demo.cs new file mode 100644 index 0000000..6a95b97 --- /dev/null +++ b/LinqStudy/IEnumerableDemo/Demo.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; + +namespace LinqStudy.IEnumerableDemo +{ + /// + /// 普通学生类 + /// + public class Student + { + public string Name { get; set; } + public int Age { get; set; } + } + + /// + /// 实现可枚举接口 + /// + public class Students : IEnumerable + { + private Student[] arrStudent; + + public Students(Student[] collection) + { + arrStudent = collection; + } + + public Student this[int index] + { + get { return arrStudent[index]; } + } + + public int Count + { + get { return arrStudent.Length; } + } + + /// + /// 实现接口 + /// + /// + IEnumerator IEnumerable.GetEnumerator() + { + return new StudentIterator(this); + } + } + + /// + /// 实现枚举器接口 + /// + public class StudentIterator : IEnumerator + { + private Students arrStudents; + private int idx; + + internal StudentIterator(Students collection) + { + this.arrStudents = collection; + idx = -1; + } + + object IEnumerator.Current + { + get { return arrStudents[idx]; } + } + + public bool MoveNext() + { + idx++; + return idx < arrStudents.Count; + } + + public void Reset() + { + idx = -1; + } + public void Dispose() { } + } + + //使用 + public class UseDemo + { + public void Test() + { + Students arrStudent = new Students + ( + new Student[] + { + new Student() { Name = "张三", Age = 23 }, + new Student() { Name = "李四", Age = 24 }, + new Student() { Name = "王五", Age = 45 }, + new Student() { Name = "DDD", Age = 21 }, + new Student() { Name = "BBB", Age = 14 }, + new Student() { Name = "CCC", Age = 3 }, + new Student() { Name = "AAA", Age = 22 }, + new Student() { Name = "BBB", Age = 55 }, + new Student() { Name = "CCC", Age = 43 }, + } + ); + + foreach (Student obj in arrStudent) + { + Console.WriteLine("姓名:" + obj.Name + ",年龄:" + obj.Age); + } + } + } +}