master
bicijinlian 6 years ago
parent 7a9e00dc61
commit 7f874724a5

@ -20,10 +20,6 @@
<ItemGroup> <ItemGroup>
<Folder Include="ExpressionTree\" /> <Folder Include="ExpressionTree\" />
<Folder Include="LinqToDataSet\" />
<Folder Include="LinqToSQL\" />
<Folder Include="LinqToXml\" />
<Folder Include="Util\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -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>
@ -50,14 +55,16 @@ namespace LinqStudy.Test.LinqToObject
public void OfType_Test() 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<int>() var values = from i in ints.OfType<int>()
select i; select i;
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或存在不能转换的元素则异常。
@ -70,7 +77,7 @@ namespace LinqStudy.Test.LinqToObject
var values = ints.Cast<string>(); var values = ints.Cast<string>();
Assert.Equal(new List<string>() {"1","2","3","4","5"}, values.ToList()); Assert.Equal(new List<string>() { "1", "2", "3", "4", "5" }, values.ToList());
} }
[Fact] [Fact]
@ -79,7 +86,7 @@ namespace LinqStudy.Test.LinqToObject
//动态数组做为数据源 //动态数组做为数据源
ArrayList ints = null; ArrayList ints = null;
Assert.Throws<ArgumentNullException>(()=> { Assert.Throws<ArgumentNullException>(() => {
ints.Cast<string>(); ints.Cast<string>();
}); });
} }
@ -88,13 +95,15 @@ namespace LinqStudy.Test.LinqToObject
public void Cast_InvalidCastException_Test() public void Cast_InvalidCastException_Test()
{ {
//动态数组做为数据源 //动态数组做为数据源
ArrayList ints = new ArrayList() { 1,"2",3,"4"}; ArrayList ints = new ArrayList() { 1, "2", 3, "4" };
Assert.Throws<InvalidCastException>(() => { Assert.Throws<InvalidCastException>(() => {
ints.Cast<string>().ToList(); ints.Cast<string>().ToList();
}); });
} }
#endregion
#region ToList
/// <summary> /// <summary>
/// ToList将IEnumerable<T>转换为List<T>, 立即执行。 /// ToList将IEnumerable<T>转换为List<T>, 立即执行。
/// 特别注意:此操作破坏"延迟执行",变为立即执行; /// 特别注意:此操作破坏"延迟执行",变为立即执行;
@ -103,7 +112,7 @@ namespace LinqStudy.Test.LinqToObject
[Fact] [Fact]
public void ToList_Test() public void ToList_Test()
{ {
List<string> email = new List<string>() { "abc@163.com","bcd@126.com","111@163.com"}; List<string> email = new List<string>() { "abc@163.com", "bcd@126.com", "111@163.com" };
var query = email.Where(q => q.Contains("@163.")).ToList(); var query = email.Where(q => q.Contains("@163.")).ToList();
@ -145,43 +154,222 @@ 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);
private DataTable GetDataTable() 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() ;
DataTable dt=new DataTable(); Assert.Equal("a1@163.com", group1_firstEmail);
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"); [Fact]
col_name.AllowDBNull=true; public void ToLookup_keySelector_elementSelector_Test()
col_name.AutoIncrement=false; {
col_name.DataType=typeof(string); var persons = new List<Employee>()
col_name.DefaultValue=string.Empty; {
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" } }
};
DataColumn col_age=new DataColumn("Age"); var lookup = persons.ToLookup(q => q.Id, e=>e.Emails);
col_age.AllowDBNull=false; var group1_Emails = lookup[1].FirstOrDefault();
col_age.AutoIncrement=false; var group1_Emails_first = group1_Emails.FirstOrDefault();
col_age.DataType=typeof(int);
col_age.DefaultValue=20; 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()
{
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_id);
dt.Columns.Add(col_name); dt.Columns.Add(col_name);
dt.Columns.Add(col_age); dt.Columns.Add(col_age);
var row_one = dt.NewRow(); var row_one = dt.NewRow();
row_one[1]="王高峰"; row_one[1] = "王高峰";
row_one[2]=21; row_one[2] = 21;
var row_2 = dt.NewRow(); var row_2 = dt.NewRow();
row_2[1]="王向前"; row_2[1] = "王向前";
row_2[2]=32; row_2[2] = 32;
dt.Rows.Add(row_one); dt.Rows.Add(row_one);
dt.Rows.Add(row_2); dt.Rows.Add(row_2);
@ -209,6 +397,6 @@ namespace LinqStudy.Test.LinqToObject
return arrStudent; return arrStudent;
} }
#endregion
} }
} }

Loading…
Cancel
Save