diff --git a/LinqStudy.Test/LinqToObject/OrderTest.cs b/LinqStudy.Test/LinqToObject/OrderTest.cs
new file mode 100644
index 0000000..96ae0f3
--- /dev/null
+++ b/LinqStudy.Test/LinqToObject/OrderTest.cs
@@ -0,0 +1,300 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Linq;
+using System.Linq.Expressions;
+
+using Xunit;
+
+namespace LinqStudy.Test.LinqToObject
+{
+ ///
+ /// 排序操作符
+ /// 数据源为null时,均引发ArgumentNullException异常
+ ///
+ public class OrderTest
+ {
+ #region OrderBy
+
+ ///
+ /// OrderBy:升序排序(从小到大);延迟执行
+ ///
+ [Fact]
+ public void OrderBy_Test()
+ {
+ List peoples = new List()
+ {
+ new Person(){ Id = 1, Name = "张三", Age=33 },
+ new Person(){ Id = 3, Name = "李四", Age=65 },
+ new Person(){ Id = 2, Name = "王五", Age=17 },
+ };
+
+ var oprationItem = peoples.OrderBy(p => p.Id);
+
+ var firtId = oprationItem.First().Id;
+ var lastId = oprationItem.Last().Id;
+
+ Assert.Equal(1,firtId);
+ Assert.Equal(3, lastId);
+ }
+
+ ///
+ /// 延迟执行
+ ///
+ [Fact]
+ public void OrderBy_Delay_Test()
+ {
+ List peoples = new List()
+ {
+ new Person(){ Id = 1, Name = "张三", Age=33 },
+ new Person(){ Id = 3, Name = "李四", Age=65 },
+ new Person(){ Id = 2, Name = "王五", Age=17 },
+ };
+
+ var oprationItem = peoples.OrderBy(p => p.Id);
+
+ var addItem = new Person() { Id = 4, Name = "赵立", Age = 87 };
+ peoples.Add(addItem);
+
+ var firtId = oprationItem.First().Id;
+ var lastId = oprationItem.Last().Id;
+
+ Assert.Equal(1, firtId);
+ Assert.Equal(4, lastId);
+ }
+
+ ///
+ /// 重载:自定义Comparable比较器
+ ///
+ [Fact]
+ public void OrderBy_CustomComparable_Test()
+ {
+ List peoples = new List()
+ {
+ new Person(){ Id = 1, Name = "张三", Age=33 },
+ new Person(){ Id = 3, Name = "李四", Age=65 },
+ new Person(){ Id = 2, Name = "王五", Age=17 },
+ };
+
+ var oprationItem = peoples.OrderBy(p => p, new PersonComparer());
+
+ var firtId = oprationItem.First().Id;
+ var lastId = oprationItem.Last().Id;
+
+ Assert.Equal(1, firtId);
+ Assert.Equal(3, lastId);
+ }
+ #endregion
+
+ #region OrderByDescending
+ ///
+ /// OrderByDescending:降序排序(从大到步);延迟执行
+ ///
+ [Fact]
+ public void OrderByDescending_Test()
+ {
+ List peoples = new List()
+ {
+ new Person(){ Id = 1, Name = "张三", Age=33 },
+ new Person(){ Id = 3, Name = "李四", Age=65 },
+ new Person(){ Id = 2, Name = "王五", Age=17 },
+ };
+
+ var oprationItem = peoples.OrderByDescending(p => p.Id);
+
+ var firtId = oprationItem.First().Id;
+ var lastId = oprationItem.Last().Id;
+
+ Assert.Equal(3, firtId);
+ Assert.Equal(1, lastId);
+ }
+
+ ///
+ /// 延迟执行
+ ///
+ [Fact]
+ public void OrderByDescending_Delay_Test()
+ {
+ List peoples = new List()
+ {
+ new Person(){ Id = 1, Name = "张三", Age=33 },
+ new Person(){ Id = 3, Name = "李四", Age=65 },
+ new Person(){ Id = 2, Name = "王五", Age=17 },
+ };
+
+ var oprationItem = peoples.OrderByDescending(p => p.Id);
+
+ var addItem = new Person() { Id = 4, Name = "赵立", Age = 87 };
+ peoples.Add(addItem);
+
+ var firtId = oprationItem.First().Id;
+ var lastId = oprationItem.Last().Id;
+
+ Assert.Equal(4, firtId);
+ Assert.Equal(1, lastId);
+ }
+
+ ///
+ /// 重载:自定义Comparable比较器
+ ///
+ [Fact]
+ public void OrderByDescending_CustomComparable_Test()
+ {
+ List peoples = new List()
+ {
+ new Person(){ Id = 1, Name = "张三", Age=33 },
+ new Person(){ Id = 3, Name = "李四", Age=65 },
+ new Person(){ Id = 2, Name = "王五", Age=17 },
+ };
+
+ var oprationItem = peoples.OrderByDescending(p => p, new PersonComparer());
+
+ var firtId = oprationItem.First().Id;
+ var lastId = oprationItem.Last().Id;
+
+ Assert.Equal(3, firtId);
+ Assert.Equal(1, lastId);
+ }
+ #endregion
+
+ #region ThenBy
+ ///
+ /// ThenBy:按次要关键字,进行升序排序(从小到大);延迟执行
+ ///
+ [Fact]
+ public void ThenBy_Test()
+ {
+ List peoples = new List()
+ {
+ new Person(){ Id = 1, Name = "张三", Age=44 },
+ new Person(){ Id = 1, Name = "李四", Age=33 },
+ new Person(){ Id = 2, Name = "王五", Age=22 },
+ new Person(){ Id = 2, Name = "赵立", Age=11 },
+ };
+
+ //先按Id排序,Id相同的,再按年龄排序。
+ var oprationItem = peoples.OrderBy(p => p.Id).ThenBy(p => p.Age).Select(p => p.Name);
+ var expected = new List() { "李四", "张三", "赵立", "王五" };
+
+ Assert.Equal(expected, oprationItem);
+ }
+
+ ///
+ /// 延迟执行
+ ///
+ [Fact]
+ public void ThenBy_Delay_Test()
+ {
+ List peoples = new List()
+ {
+ new Person(){ Id = 1, Name = "张三", Age=44 },
+ new Person(){ Id = 1, Name = "李四", Age=33 },
+ new Person(){ Id = 2, Name = "王五", Age=22 },
+ new Person(){ Id = 2, Name = "赵立", Age=11 },
+ };
+
+ var oprationItem = peoples.OrderBy(p => p.Id).ThenBy(p => p.Age);
+
+ var addItme = new Person() { Id = 1, Name = "刘能", Age = 2 };
+ peoples.Add(addItme);
+
+ var actual = oprationItem.Select(p => p.Name).ToList();
+ var expected = new List() { "刘能", "李四", "张三", "赵立", "王五" };
+
+ Assert.Equal(expected, actual);
+ }
+
+ ///
+ /// 重载:自定义Comparable比较器
+ ///
+ [Fact]
+ public void ThenBy_CustomComparable_Test()
+ {
+ List peoples = new List()
+ {
+ new Person(){ Id = 1, Name = "张三", Age=44 },
+ new Person(){ Id = 1, Name = "李四", Age=33 },
+ new Person(){ Id = 2, Name = "王五", Age=22 },
+ new Person(){ Id = 2, Name = "赵立", Age=11 },
+ };
+
+ var oprationItem = peoples.OrderBy(p => p, new PersonComparer()).ThenBy(p=>p.Age,Comparer.Default);
+
+ var actual = oprationItem.Select(p => p.Name).ToList();
+ var expected = new List() {"李四", "张三", "赵立", "王五" };
+
+ Assert.Equal(expected, actual);
+ }
+ #endregion
+
+ #region ThenByDescending
+ ///
+ /// ThenByDescending:按次要关键字,进行降序排序(从大到小);延迟执行
+ ///
+ [Fact]
+ public void ThenByDescending_Test()
+ {
+ List peoples = new List()
+ {
+ new Person(){ Id = 1, Name = "张三", Age=44 },
+ new Person(){ Id = 1, Name = "李四", Age=33 },
+ new Person(){ Id = 2, Name = "王五", Age=22 },
+ new Person(){ Id = 2, Name = "赵立", Age=11 },
+ };
+
+ //先按Id排序,Id相同的,再按年龄排序。
+ var oprationItem = peoples.OrderBy(p => p.Id).ThenByDescending(p => p.Age).Select(p => p.Name);
+ var expected = new List() {"张三","李四","王五", "赵立"};
+
+ Assert.Equal(expected, oprationItem);
+ }
+
+ ///
+ /// 延迟执行
+ ///
+ [Fact]
+ public void ThenByDescending_Delay_Test()
+ {
+ List peoples = new List()
+ {
+ new Person(){ Id = 1, Name = "张三", Age=44 },
+ new Person(){ Id = 1, Name = "李四", Age=33 },
+ new Person(){ Id = 2, Name = "王五", Age=22 },
+ new Person(){ Id = 2, Name = "赵立", Age=11 },
+ };
+
+ var oprationItem = peoples.OrderBy(p => p.Id).ThenByDescending(p => p.Age);
+
+ var addItme = new Person() { Id = 1, Name = "刘能", Age = 2 };
+ peoples.Add(addItme);
+
+ var actual = oprationItem.Select(p => p.Name).ToList();
+ var expected = new List() { "张三", "李四", "刘能", "王五","赵立" };
+
+ Assert.Equal(expected, actual);
+ }
+
+ ///
+ /// 重载:自定义Comparable比较器
+ ///
+ [Fact]
+ public void ThenByDescending_CustomComparable_Test()
+ {
+ List peoples = new List()
+ {
+ new Person(){ Id = 1, Name = "张三", Age=44 },
+ new Person(){ Id = 1, Name = "李四", Age=33 },
+ new Person(){ Id = 2, Name = "王五", Age=22 },
+ new Person(){ Id = 2, Name = "赵立", Age=11 },
+ };
+
+ var oprationItem = peoples.OrderBy(p => p, new PersonComparer()).ThenByDescending(p => p.Age, Comparer.Default);
+
+ var actual = oprationItem.Select(p => p.Name).ToList();
+ var expected = new List() { "张三","李四", "王五", "赵立",};
+
+ Assert.Equal(expected, actual);
+ }
+ #endregion
+ }
+}
diff --git a/LinqStudy.Test/LinqToObject/QuantifierTest.cs b/LinqStudy.Test/LinqToObject/QuantifierTest.cs
index 0d13c7b..94b23d2 100644
--- a/LinqStudy.Test/LinqToObject/QuantifierTest.cs
+++ b/LinqStudy.Test/LinqToObject/QuantifierTest.cs
@@ -281,8 +281,8 @@ namespace LinqStudy.Test.LinqToObject
var people = new Person() { Id = 3, Name = "小清闲", Age = 88 };
//自定义比较器:Id相同即为同一个对象
- PersonEqualId equalId = new PersonEqualId();
- var anyQuery = peoples.Contains(people, new PersonEqualId());
+ PersonEqualityComparerById equalId = new PersonEqualityComparerById();
+ var anyQuery = peoples.Contains(people, new PersonEqualityComparerById());
//比较器相等,即包含
Assert.True(anyQuery);
diff --git a/LinqStudy.Test/LinqToObject/SequenceEqualTest.cs b/LinqStudy.Test/LinqToObject/SequenceEqualTest.cs
index 49c318a..79dd73d 100644
--- a/LinqStudy.Test/LinqToObject/SequenceEqualTest.cs
+++ b/LinqStudy.Test/LinqToObject/SequenceEqualTest.cs
@@ -192,7 +192,7 @@ namespace LinqStudy.Test.LinqToObject
new Person(){Id=1,Name="李四",Age=20},
};
- Assert.True(sequence1.SequenceEqual(sequence2, new PersonEqual()));
+ Assert.True(sequence1.SequenceEqual(sequence2, new PersonEqualityComparer()));
}
}
}
diff --git a/LinqStudy.Test/LinqToObject/标准查询操作符.md b/LinqStudy.Test/LinqToObject/标准查询操作符.md
index 7101401..f1bcd91 100644
--- a/LinqStudy.Test/LinqToObject/标准查询操作符.md
+++ b/LinqStudy.Test/LinqToObject/标准查询操作符.md
@@ -18,7 +18,7 @@
## 分组操作符
-## 串联操作符
+## 集合操作符
## 聚合操作符
@@ -31,8 +31,11 @@
## 相等操作符
## 量词操作符
+ Any
-## 分割操作符
+ All
+
+## 分区操作符
Take 返回序列中,从0开始的连续指定项数据子序列;延迟执行。
TakeWhile
\ No newline at end of file
diff --git a/LinqStudy/Models/PersonCompare.cs b/LinqStudy/Models/PersonCompare.cs
new file mode 100644
index 0000000..d2eec98
--- /dev/null
+++ b/LinqStudy/Models/PersonCompare.cs
@@ -0,0 +1,47 @@
+using System;
+using System.Collections.Generic;
+
+namespace LinqStudy
+{
+ ///
+ /// PersonȽIdΪȽϱ
+ ///
+ public class PersonComparer : IComparer
+ {
+ int IComparer.Compare(Person x, Person y)
+ {
+ int compareResult = 0;
+
+ if (x == null && y == null)
+ {
+ compareResult = 0;
+ }
+
+ else if (x == null)
+ {
+ compareResult = 1;
+ }
+ else if (y == null)
+ {
+ compareResult = -1;
+ }
+ else
+ {
+ if (x.Id == y.Id)
+ {
+ compareResult = 0;
+ }
+ else if (x.Id > y.Id)
+ {
+ compareResult = 1;
+ }
+ else
+ {
+ compareResult = -1;
+ }
+ }
+
+ return compareResult;
+ }
+ }
+}
diff --git a/LinqStudy/Models/PersonEqual.cs b/LinqStudy/Models/PersonEqualityComparer.cs
similarity index 84%
rename from LinqStudy/Models/PersonEqual.cs
rename to LinqStudy/Models/PersonEqualityComparer.cs
index 86237d0..7114f9e 100644
--- a/LinqStudy/Models/PersonEqual.cs
+++ b/LinqStudy/Models/PersonEqualityComparer.cs
@@ -5,10 +5,10 @@ using System.Text;
namespace LinqStudy
{
///
- /// Person自定义比较器
+ /// Person 自定义相等比较器
/// 各属性均同,则相等.有一个Null值则不相等。
///
- public class PersonEqual : IEqualityComparer
+ public class PersonEqualityComparer : IEqualityComparer
{
bool IEqualityComparer.Equals(Person x, Person y)
{
diff --git a/LinqStudy/Models/PersonEqualId.cs b/LinqStudy/Models/PersonEqualityComparerById.cs
similarity index 81%
rename from LinqStudy/Models/PersonEqualId.cs
rename to LinqStudy/Models/PersonEqualityComparerById.cs
index 7a7b8c6..c697ab8 100644
--- a/LinqStudy/Models/PersonEqualId.cs
+++ b/LinqStudy/Models/PersonEqualityComparerById.cs
@@ -5,10 +5,10 @@ using System.Text;
namespace LinqStudy
{
///
- /// Person 自定义ID比较器
+ /// Person 自定义ID相等比较器
/// Id相同,则视为相同
///
- public class PersonEqualId : IEqualityComparer
+ public class PersonEqualityComparerById : IEqualityComparer
{
bool IEqualityComparer.Equals(Person x, Person y)
{