分组操作符

master
bicijinlian 5 years ago
parent 4a1f9c712f
commit 8f91cbed01

@ -0,0 +1,168 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using LinqStudy;
namespace LinqStudy.Test.LinqToObject
{
/// <summary>
/// 分组操作符
/// </summary>
public class GroupTest
{
/// <summary>
/// GroupBy: 根据指定的键选择器函数对序列的元素进行分组;延迟执行。
/// 注意Key即分组依据可以常用的值类型也可以是引用类型(可以自定义比较器))
/// </summary>
[Fact]
public void GroupBy_Default_Test()
{
List<Person> peoples=new List<Person>()
{
new Person(){Id=1, Name="Andy", Age=10},
new Person(){Id=2, Name="Bab", Age=20},
new Person(){Id=3, Name="Babs", Age=20},
new Person(){Id=4, Name="Cara", Age=30},
new Person(){Id=5, Name="Carlton", Age=30},
new Person(){Id=6, Name="Cora", Age=30},
new Person(){Id=7, Name="Dawn", Age=40},
new Person(){Id=8, Name="Debby", Age=40},
new Person(){Id=9, Name="Dotty", Age=40},
new Person(){Id=10, Name="Dove", Age=40},
};
var group = peoples.GroupBy(p=>p.Age);
var groupCount = group.Count();
Assert.Equal(4,groupCount);
}
/// <summary>
/// 延迟执行
/// </summary>
[Fact]
public void GroupBy_Delay_Test()
{
List<Person> peoples=new List<Person>()
{
new Person(){Id=1, Name="Andy", Age=10},
new Person(){Id=2, Name="Bab", Age=20},
new Person(){Id=3, Name="Babs", Age=20},
new Person(){Id=4, Name="Cara", Age=30},
new Person(){Id=5, Name="Carlton", Age=30},
new Person(){Id=6, Name="Cora", Age=30},
};
var group = peoples.GroupBy(p=>p.Age);
//改变数据源
List<Person> peoples2=new List<Person>()
{
new Person(){Id=7, Name="Dawn", Age=40},
new Person(){Id=8, Name="Debby", Age=40},
new Person(){Id=9, Name="Dotty", Age=40},
new Person(){Id=10, Name="Dove", Age=40},
};
peoples.AddRange(peoples2);
//受数据源变化的影响,说明是延迟执行。
var groupCount = group.Count();
Assert.Equal(4,groupCount);
}
/// <summary>
/// 重载keySelector comparer
/// key是引用类型使用自定义比较器
/// </summary>
[Fact]
public void GroupBy_keySelector_comparer_Test()
{
List<Teacher> peoples=new List<Teacher>()
{
new Teacher(){Id=1, Name="河南周口人", Hometown=new Hometown(){Province="河南", City="周口"}},
new Teacher(){Id=2, Name="俺河南周口哩", Hometown=new Hometown(){Province="河南",City="周口"}},
new Teacher(){Id=3, Name="上海宝山人", Hometown=new Hometown(){Province="上海",City="宝山"}},
new Teacher(){Id=4, Name="上海松江人", Hometown=new Hometown(){Province="上海",City="松江"}},
};
var group = peoples.GroupBy(p=>p.Hometown,new HometownEqualityComparer());
var groupFirstNames=group.First().Select(q=>q.Name).ToList();
var expected=new List<string>(){"河南周口人","俺河南周口哩"};
Assert.Equal(expected,groupFirstNames);
}
/// <summary>
/// 重载keySelector elementSelector
/// key是引用类型使用自定义比较器
/// </summary>
[Fact]
public void GroupBy_keySelector_elementSelector_Test()
{
List<Teacher> peoples=new List<Teacher>()
{
new Teacher(){Id=1, Name="河南周口人", Hometown=new Hometown(){Province="河南", City="周口"}},
new Teacher(){Id=1, Name="俺河南周口哩", Hometown=new Hometown(){Province="河南",City="周口"}},
new Teacher(){Id=2, Name="上海宝山人", Hometown=new Hometown(){Province="上海",City="宝山"}},
new Teacher(){Id=3, Name="上海松江人", Hometown=new Hometown(){Province="上海",City="松江"}},
};
var group = peoples.GroupBy(p=>p.Id,t=>t.Name).ToList();
var groupFirstNames=group[2].FirstOrDefault();
var expected="上海松江人";
Assert.Equal(expected,groupFirstNames);
}
/// <summary>
/// 重载keySelector elementSelector comparer
/// </summary>
[Fact]
public void GroupBy_keySelector_elementSelector_comparer_Test()
{
List<Teacher> peoples=new List<Teacher>()
{
new Teacher(){Id=1, Name="河南周口人", Hometown=new Hometown(){Province="河南", City="周口"}},
new Teacher(){Id=2, Name="俺河南周口哩", Hometown=new Hometown(){Province="河南",City="周口"}},
new Teacher(){Id=3, Name="上海宝山人", Hometown=new Hometown(){Province="上海",City="宝山"}},
new Teacher(){Id=4, Name="上海松江人", Hometown=new Hometown(){Province="上海",City="松江"}},
};
var group = peoples.GroupBy(p=>p.Hometown,t=>t.Name,new HometownEqualityComparer()).ToList();
var groupFirstNames=group[2].FirstOrDefault();
var expected="上海松江人";
Assert.Equal(expected,groupFirstNames);
}
[Fact]
public void GroupBy_keySelector_resultSelector_Test()
{
List<Teacher> peoples=new List<Teacher>()
{
new Teacher(){Id=1, Name="河南周口人", Hometown=new Hometown(){Province="河南", City="周口"}},
new Teacher(){Id=1, Name="俺河南周口哩", Hometown=new Hometown(){Province="河南",City="周口"}},
new Teacher(){Id=3, Name="上海宝山人", Hometown=new Hometown(){Province="上海",City="宝山"}},
new Teacher(){Id=4, Name="上海松江人", Hometown=new Hometown(){Province="上海",City="松江"}},
};
var group = peoples.GroupBy(p=>p.Id, (key,teacher) => teacher.Select(t=>t.Name)).ToList();
var groupFirstNames=group[2].FirstOrDefault();
var expected="上海松江人";
Assert.Equal(expected,groupFirstNames);
}
}
}

@ -31,11 +31,13 @@
## 相等操作符
## 量词操作符
Any
All
## 分区操作符
Take 返回序列中,从0开始的连续指定项数据子序列延迟执行。
TakeWhile
TakeWhile

@ -0,0 +1,21 @@
using System;
namespace LinqStudy
{
/// <summary>
/// 家乡类
/// </summary>
public class Hometown
{
/// <summary>
/// 省份
/// </summary>
/// <value></value>
public string Province{get;set;}
///<summary>
/// 城市
/// </summary>
public string City{get;set;}
}
}

@ -0,0 +1,44 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace LinqStudy
{
/// <summary>
/// 家乡 比较器
/// </summary>
public class HometownCompare : IComparer<Hometown>
{
int IComparer<Hometown>.Compare(Hometown x, Hometown 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.Province == y.Province)
{
compareResult = string.Compare(x.City,y.City);
}
else
{
compareResult = string.Compare(x.Province,y.Province);
}
}
return compareResult;
}
}
}

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
namespace LinqStudy
{
/// <summary>
/// 家乡类 相等比较器
/// </summary>
public class HometownEqualityComparer : IEqualityComparer<Hometown>
{
bool IEqualityComparer<Hometown>.Equals(Hometown x, Hometown y)
{
if (x==null || y==null)
{
return false;
}
return x.Province == y.Province && x.City == y.City;
}
int IEqualityComparer<Hometown>.GetHashCode(Hometown obj)
{
return (obj.Province+obj.City).GetHashCode();
}
}
}

@ -3,7 +3,7 @@ using System;
namespace LinqStudy
{
/// <summary>
/// 人
/// 人
/// </summary>
public class Person
{

@ -4,7 +4,7 @@ using System.Collections.Generic;
namespace LinqStudy
{
/// <summary>
/// Person比较器Id为比较标准
/// Person 比较器
/// </summary>
public class PersonComparer : IComparer<Person>
{

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
namespace LinqStudy
{
public class PersonEqualityComparerByName : IEqualityComparer<Person>
{
bool IEqualityComparer<Person>.Equals(Person x, Person y)
{
if (x==null || y==null)
{
return false;
}
return x.Name == y.Name;
}
int IEqualityComparer<Person>.GetHashCode(Person obj)
{
return (obj.Name).GetHashCode();
}
}
}

@ -0,0 +1,25 @@
using System;
namespace LinqStudy
{
/// <summary>
/// 老师
/// </summary>
public class Teacher
{
/// <summary>
/// Id
/// </summary>
public int Id {get;set;}
/// <summary>
/// 姓名
/// </summary>
public string Name{get;set;}
/// <summary>
/// 家乡
/// </summary>
public Hometown Hometown{get;set;}
}
}

@ -0,0 +1,61 @@
using System;
using System.Collections;
using System.Collections.Generic;
using LinqStudy;
namespace LinqStudy
{
/// <summary>
/// 家乡 比较器
/// </summary>
public class TeacherCompare : IComparer<Teacher>
{
int IComparer<Teacher>.Compare(Teacher x, Teacher 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)
{
if(x.Name==y.Name)
{
IComparer<Hometown> homeCompare= new HometownCompare();
compareResult= homeCompare.Compare(x.Hometown, y.Hometown);
}
else
{
compareResult = string.Compare(x.Name,y.Name);
}
}
else
{
if(x.Id>y.Id)
{
compareResult =1;
}
else
{
compareResult = -1;
}
}
}
return compareResult;
}
}
}
Loading…
Cancel
Save