SelectMany

来源:互联网 发布:内衣取名知乎 编辑:程序博客网 时间:2024/06/14 00:52

        private void button2_Click(object sender, EventArgs e)
        {
            List<List<int>> numbers = new List<List<int>>()
            {
              new List<int>{1,2,3},
              new List<int>{4,5,6},
              new List<int>{7,8,9}
            };

            var result = numbers.SelectMany(collection=>collection);

            foreach(var item in result)
            {
                Console.WriteLine(item);
            }
        }

foreach (string i in "123".SelectMany(x => "123".Select(y => string.Format("a{0}b{1}c", x, y))))
    Console.WriteLine(i);

 

1
2
3
4
5
6
7
8
9
a1b1c
a1b2c
a1b3c
a2b1c
a2b2c
a2b3c
a3b1c
a3b2c
a3b3c

            string old="1/200;2/300;3/300;4/500;20/600;23/700;24/700;25/700;26/300";
            var res1 = string.Join(";",
                old.Split(';').Select(p =>
                {         
                    var t = p.Split('/');
                    return new { id = t[0], num = Convert.ToInt32(t[1]) };
                })
                .GroupBy(p => p.num)
                .Select(p => string.Join("-", p.Select(c => c.id)) + "/" + p.Key.ToString())
                );
            Console.WriteLine(res1);

           

"1/200;2-3-26/300;4/500;20/600;23-24-25/700"

 

原创粉丝点击