码迷,mamicode.com
首页 > 其他好文 > 详细

08 LINQ中的SelectMany(Select Many Operator in LINQ)

时间:2016-01-17 13:29:26      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

SelectMany函数可以将返回的List展开为List。理解这句话的最好方式是通过一个例子:

public class Person
{
    public string FirstName;
    public string LastName;
    public string Gender;
    public int AnualSalary;
    public List<string> PhoneNumber;

    public static List<Person> GetAllPerson()
    {
        List<Person> personInList = new List<Person>() 
        {
            new Person()
            {
                 FirstName = "Jonh",
                 LastName = "Liu",
                 Gender = "Male",
                 AnualSalary = 70000,
                 PhoneNumber = new List<string>()
                 {
                     "0101235566",
                     "12056878445"
                 }
            },
            new Person()
            {
                 FirstName = "Sara",
                 LastName = "Guo",
                 Gender = "Famale",
                 AnualSalary = 100000,
                 PhoneNumber = new List<string>()
                 {
                     "13356985469",
                     "15536215459"
                 }
            },
            new Person()
            {
                 FirstName="Marttin",
                 LastName = "Zeng",
                 Gender = "Male",
                 AnualSalary = 80000,
                 PhoneNumber = new List<string>()
                 {
                     "15862354569",
                     "13756542698",
                     "15936545269"
                 }
            },
            new Person()
            {
                 FirstName="Sccot",
                 LastName= "Jiang",
                 Gender = "Male",
                 AnualSalary = 90000,
                 PhoneNumber = new List<string>()
                 {
                     "02388695645"
                 }
            }
        };

        return personInList;
    }
}

上面的代码中,Person类中有一个List<string>类型的名为PhoneNumber的属性。如果我们要获取性别为“男”的List<Person>的所有PhoneNumer我们可以用Select语句:

 List<Person> personInList = Person.GetAllPerson();
 IEnumerable<List<string>> marttinsPhoneNumberList = personInList.Where(p => p.Gender == "Male").Select(p => p.PhoneNumber);
 foreach (List<string> eachPhoneNumberList in marttinsPhoneNumberList)
 {
     foreach (string eachPhoneNumber in eachPhoneNumberList)
     {
         Console.WriteLine(eachPhoneNumber);
     }
     Console.WriteLine(new string(-, 20));
 }

可以看到,我们用Select函数筛选的结果是IEnumerable<List<string>>类型。这很好理解,因为PhoneNumber在Person类中本来就是一个List<string>类型。但如果我们用SelectMany函数会发生什么呢?请看下面的代码:

List<Person> personInList = Person.GetAllPerson();
IEnumerable<string> allPhoneNumberInList = personInList.Where(p => p.Gender == "Male").SelectMany(p => p.PhoneNumber);
foreach (string eachPhoneNumber in allPhoneNumberInList)
{
    Console.WriteLine(eachPhoneNumber);
}

事实上,我们必须用IEnumerable<string>来接收SelectMany的返回值!因为SelectMany会将返回的类型为List<string>的PhoneNumber进一步展开为IEnumerable<string>类型。这也就解释了文章开头的“SelectMany函数可以将返回的List展开为List”的意思。再举一个例子,如果我们运行下面的代码:

List<Person> personInList = Person.GetAllPerson();
IEnumerable<char> allCharOfLastName = personInList.Where(p => p.FirstName == "Sccot").SelectMany(p => p.LastName);
foreach (char eachChar in allCharOfLastName)
{
    Console.WriteLine(eachChar);
}

上面的代码首先选出了FirstName为"Sccot"的person,然后用SelectMany返回其LastName。注意LastName的类型为string,SelectMany返回的结果是把string展开为一个char类型的List了。

 

SelectMany还有另外的重载。例如,如果我们想同时显示FullName和对应的PhoneNumber的话,可以使用下面的代码达到目的:

List<Person> personInList = Person.GetAllPerson();
var result = personInList.Where(p => p.Gender == "Male").SelectMany(p => p.PhoneNumber, (person, phoneNumber) => new { FullName = person.FirstName + " " + person.LastName, PhoneNumber = phoneNumber });
foreach (var item in result)
{
    Console.WriteLine(string.Format("FullName:{0}\r\nPhoneNumber:{1}", item.FullName, item.PhoneNumber));
}

 

参考:http://stackoverflow.com/questions/958949/difference-between-select-and-selectmany

08 LINQ中的SelectMany(Select Many Operator in LINQ)

标签:

原文地址:http://www.cnblogs.com/kuillldan/p/5137045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!