|
|
|
@ -16,6 +16,8 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ProjectiveTest
|
|
|
|
|
{
|
|
|
|
|
#region Select
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Select投影:简单投影,一对一
|
|
|
|
|
/// </summary>
|
|
|
|
@ -26,11 +28,12 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
|
var persons = PersonManager.GetPersons();
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var maps = persons.Select(p=>p.Age).ToList();
|
|
|
|
|
|
|
|
|
|
var maps = persons.Select(p => p.Age).ToList();
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsType<List<int>>(maps);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 投影为匿名类
|
|
|
|
|
/// </summary>
|
|
|
|
@ -41,8 +44,8 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
|
var persons = PersonManager.GetPersons();
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var maps = persons.Select(p=>new {Id=p.Id, Node=$"姓名{p.Name},年龄{p.Age}."}).ToList();
|
|
|
|
|
|
|
|
|
|
var maps = persons.Select(p => new { Id = p.Id, Node = $"姓名{p.Name},年龄{p.Age}." }).ToList();
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsNotType<List<Person>>(maps);
|
|
|
|
|
}
|
|
|
|
@ -55,12 +58,15 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
|
{
|
|
|
|
|
var persons = PersonManager.GetPersons();
|
|
|
|
|
|
|
|
|
|
var maps = persons.Select((query,index)=> new KeyValuePair<int, Person>(index,query)).ToList();
|
|
|
|
|
var maps = persons.Select((query, index) => new KeyValuePair<int, Person>(index, query)).ToList();
|
|
|
|
|
var indexs = persons.Select((query, index) => index).ToList();
|
|
|
|
|
|
|
|
|
|
Assert.IsType<List<KeyValuePair<int, Person>>>(maps);
|
|
|
|
|
Assert.Equal(0,indexs[0]);
|
|
|
|
|
Assert.Equal(0, indexs[0]);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region SelectMany
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 将序列的每个元素投影到 IEnumerable<TResult> 并将结果序列合并为一个序列。
|
|
|
|
@ -82,7 +88,7 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
|
new Employee(){Id=5,Name="东升",Emails=new List<string>(){ "ebc@163.com", "ecd@163.com", "ede@163.com" }},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var maps = employees.SelectMany(q=>q.Emails).ToList();
|
|
|
|
|
var maps = employees.SelectMany(q => q.Emails).ToList();
|
|
|
|
|
|
|
|
|
|
Assert.IsType<List<string>>(maps);
|
|
|
|
|
Assert.Equal(15, maps.Count);
|
|
|
|
@ -106,7 +112,7 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
|
new Employee(){Id=5,Name="东升",Emails=new List<string>(){ "ebc@163.com", "ecd@163.com", "ede@163.com" }},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var maps = employees.SelectMany((q, idx) =>
|
|
|
|
|
var maps = employees.SelectMany((q, idx) =>
|
|
|
|
|
{
|
|
|
|
|
q.Emails.Add(idx.ToString());
|
|
|
|
|
return q.Emails;
|
|
|
|
@ -134,7 +140,7 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
|
new Employee(){ Id=5,Name="东升",Emails=new List<string>(){ "e1@163.com", "e2@163.com", "e3@163.com" } },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var maps = employees.SelectMany((employee)=> employee.Emails,(person,email)=> new { Name=person.Name,Email=email}).ToList();
|
|
|
|
|
var maps = employees.SelectMany((employee) => employee.Emails, (person, email) => new { Name = person.Name, Email = email }).ToList();
|
|
|
|
|
|
|
|
|
|
Assert.Equal(15, maps.Count);
|
|
|
|
|
}
|
|
|
|
@ -160,12 +166,12 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
|
|
|
|
|
|
var maps = employees.SelectMany
|
|
|
|
|
(
|
|
|
|
|
(employee, idx) =>
|
|
|
|
|
(employee, idx) =>
|
|
|
|
|
{
|
|
|
|
|
return new List<Employee>() { new Employee() { Id = employee.Id, Name = employee.Name + "_" + idx, Emails = employee.Emails } };
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
(person,email) => new
|
|
|
|
|
(person, email) => new
|
|
|
|
|
{
|
|
|
|
|
Name = email.Name,
|
|
|
|
|
Email = email.Emails
|
|
|
|
@ -175,26 +181,6 @@ namespace LinqStudy.Test.LinqToObject
|
|
|
|
|
|
|
|
|
|
Assert.Equal(5, maps.Count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Test()
|
|
|
|
|
{
|
|
|
|
|
var cc= DocDemo("");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Doc示例
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="aa">参数</param>
|
|
|
|
|
/// <exception cref="ArgumentNullException">
|
|
|
|
|
/// ArgumentNullException
|
|
|
|
|
/// </exception>
|
|
|
|
|
/// <example>DecDemo("dsfsadf")</example>
|
|
|
|
|
/// <seealso cref="SortedDictionary{TKey, TValue}"/>
|
|
|
|
|
/// <returns>参数+“-”</returns>
|
|
|
|
|
private string DocDemo(string aa)
|
|
|
|
|
{
|
|
|
|
|
return aa + "-";
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|