Difference Between Select and SelectMany

后端 未结 17 1314
长情又很酷
长情又很酷 2020-11-22 05:21

I\'ve been searching the difference between Select and SelectMany but I haven\'t been able to find a suitable answer. I need to learn the differenc

17条回答
  •  爱一瞬间的悲伤
    2020-11-22 05:35

    One more example how SelectMany + Select can be used in order to accumulate sub array objects data.

    Suppose we have users with they phones:

    class Phone { 
        public string BasePart = "555-xxx-xxx"; 
    }
    
    class User { 
        public string Name = "Xxxxx";
        public List Phones; 
    }
    

    Now we need to select all phones' BaseParts of all users:

    var usersArray = new List(); // array of arrays
    List allBaseParts = usersArray.SelectMany(ua => ua.Phones).Select(p => p.BasePart).ToList();
    

提交回复
热议问题