|
|
@ -18,6 +18,7 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
public class TypeConvertTest
|
|
|
|
public class TypeConvertTest
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
#region AsEnumeralbe
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// AsEnumeralbe操作符:将IEnumerable<T>输入序列转换成IEnumerable<T>的输出序列
|
|
|
|
/// AsEnumeralbe操作符:将IEnumerable<T>输入序列转换成IEnumerable<T>的输出序列
|
|
|
|
/// 主要用于将一个实现了IEnumerable<T>接口的对象转换成一个标准的IEnumerable<T>接口对象。
|
|
|
|
/// 主要用于将一个实现了IEnumerable<T>接口的对象转换成一个标准的IEnumerable<T>接口对象。
|
|
|
@ -33,7 +34,9 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(students);
|
|
|
|
Assert.NotNull(students);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region AsQueryable
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// 主要应用与Linq to sql
|
|
|
|
/// 主要应用与Linq to sql
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
@ -42,7 +45,9 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region OfType
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// OfType:从序列中筛选指定类型的元素,并构建一个序列。
|
|
|
|
/// OfType:从序列中筛选指定类型的元素,并构建一个序列。
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
@ -57,7 +62,9 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal(new List<int>() { 1, 3, 5 }, values.ToList());
|
|
|
|
Assert.Equal(new List<int>() { 1, 3, 5 }, values.ToList());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Cast
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Cast:将数据源中元素,强制转换成指定的类型。
|
|
|
|
/// Cast:将数据源中元素,强制转换成指定的类型。
|
|
|
|
/// 数据源null或存在不能转换的元素,则异常。
|
|
|
|
/// 数据源null或存在不能转换的元素,则异常。
|
|
|
@ -94,7 +101,9 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
ints.Cast<string>().ToList();
|
|
|
|
ints.Cast<string>().ToList();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region ToList
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// ToList:将IEnumerable<T>转换为List<T>, 立即执行。
|
|
|
|
/// ToList:将IEnumerable<T>转换为List<T>, 立即执行。
|
|
|
|
/// 特别注意:此操作破坏"延迟执行",变为立即执行;
|
|
|
|
/// 特别注意:此操作破坏"延迟执行",变为立即执行;
|
|
|
@ -145,8 +154,187 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
//元素对象不是同一个元素
|
|
|
|
//元素对象不是同一个元素
|
|
|
|
Assert.NotSame(persons, person2);
|
|
|
|
Assert.NotSame(persons, person2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region ToDictionary
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// ToDictionar转换字典,立即执行并保存快照。
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void ToDictionary_keySelector_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
List<Person> persons = new List<Person>()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
new Person() {Id=1, Name="gaofeng",Age = 32 },
|
|
|
|
|
|
|
|
new Person() {Id=2, Name="zhangsan",Age = 25 },
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var dic = persons.ToDictionary(p => p.Id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal(1, dic[1].Id);
|
|
|
|
|
|
|
|
Assert.Equal(2, dic[2].Id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void ToDictionary_keySelector_comparer_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
List<Person> persons = new List<Person>()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
new Person() {Id=1, Name="gaofeng",Age = 32 },
|
|
|
|
|
|
|
|
new Person() {Id=2, Name="zhangsan",Age = 25 },
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IEqualityComparer<Person> equal = new PersonEqual();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var dic = persons.ToDictionary<Person, int>(p => p.Id, EqualityComparer<int>.Default);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal("gaofeng", dic[1].Name);
|
|
|
|
|
|
|
|
Assert.Equal("zhangsan", dic[2].Name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void ToDictionary_keySelector_elementSelector_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
List<Person> persons = new List<Person>()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
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<Person> persons = new List<Person>()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
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<int>.Default);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal("gaofeng", dic[1]);
|
|
|
|
|
|
|
|
Assert.Equal("zhangsan", dic[2]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void ToDictionary_ArgumentException_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
List<Person> persons = new List<Person>()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
new Person() {Id=1, Name="gaofeng",Age = 32 },
|
|
|
|
|
|
|
|
new Person() {Id=1, Name="zhangsan",Age = 25 },
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Throws<ArgumentException>(() => {
|
|
|
|
|
|
|
|
persons.ToDictionary(p => p.Id, s => s.Name);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void ToDictionary_ArgumentNullException_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
List<Person> persons = new List<Person>()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
new Person() {Id=1, Name=null,Age = 32 },
|
|
|
|
|
|
|
|
new Person() {Id=1, Name="zhangsan",Age = 25 },
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Throws<ArgumentNullException>(() => {
|
|
|
|
|
|
|
|
persons.ToDictionary(p => p.Name, s => s.Name);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void ToDictionary_DataSource_NullException_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
List<Person> persons = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Throws<ArgumentNullException>(() => {
|
|
|
|
|
|
|
|
persons.ToDictionary(p => p.Name, s => s.Name);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region ToLookup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// ToLookup:根据指定的键选择器函数从IEnumerable<T>创建Lookup<T>
|
|
|
|
|
|
|
|
/// Lookup<T>对象表示一个一对多的字典,定义了一个由键和序列组成的元组,类似GroupJoin返回的结果。
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void ToLookup_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var persons = new List<Employee>()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
new Employee(){ Id=1, Name="gaofeng",Emails=new List<string>(){"a1@163.com","a2@163.com","a3@163.com"} },
|
|
|
|
|
|
|
|
new Employee(){ Id=1, Name="zhangsan",Emails=new List<string>(){ "b1@163.com", "b2@163.com", "b3@163.com" } },
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
new Employee(){ Id=2, Name="gaofeng2",Emails=new List<string>(){"c1@163.com","c2@163.com","c3@163.com"} },
|
|
|
|
|
|
|
|
new Employee(){ Id=2, Name="zhangsan2",Emails=new List<string>(){ "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<Employee>()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
new Employee(){ Id=1, Name="gaofeng",Emails=new List<string>(){"a1@163.com","a2@163.com","a3@163.com"} },
|
|
|
|
|
|
|
|
new Employee(){ Id=1, Name="zhangsan",Emails=new List<string>(){ "b1@163.com", "b2@163.com", "b3@163.com" } }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var lookup = persons.ToLookup(q => q.Id,EqualityComparer<int>.Default);
|
|
|
|
|
|
|
|
var group1_firstEmail = lookup[1].FirstOrDefault().Emails.FirstOrDefault() ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal("a1@163.com", group1_firstEmail);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void ToLookup_keySelector_elementSelector_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var persons = new List<Employee>()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
new Employee(){ Id=1, Name="gaofeng",Emails=new List<string>(){"a1@163.com","a2@163.com","a3@163.com"} },
|
|
|
|
|
|
|
|
new Employee(){ Id=1, Name="zhangsan",Emails=new List<string>(){ "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<Employee>()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
new Employee(){ Id=1, Name="gaofeng",Emails=new List<string>(){"a1@163.com","a2@163.com","a3@163.com"} },
|
|
|
|
|
|
|
|
new Employee(){ Id=1, Name="zhangsan",Emails=new List<string>(){ "b1@163.com", "b2@163.com", "b3@163.com" } }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var lookup = persons.ToLookup(q => q.Name, e => e.Emails,EqualityComparer<string>.Default);
|
|
|
|
|
|
|
|
var group1_Emails = lookup["gaofeng"].FirstOrDefault();
|
|
|
|
|
|
|
|
var group1_Emails_first = group1_Emails.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal("a1@163.com", group1_Emails_first);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region private
|
|
|
|
|
|
|
|
|
|
|
|
private DataTable GetDataTable()
|
|
|
|
private DataTable GetDataTable()
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -209,6 +397,6 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
|
|
|
|
|
|
|
|
return arrStudent;
|
|
|
|
return arrStudent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|