ForEach :对 System.Collections.Generic.List<T> 的每个元素执行指定操作

来源:互联网 发布:千牛mac版不响 编辑:程序博客网 时间:2024/05/16 10:08

效果:

Dep,Arr,Cabin字段进行组合

using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Test.Con{    class Program    {        static void Main(string[] args)        {            List<Policy> list = new List<Policy>();            for (int i = 0; i < 30000; i++)            {                list.Add(new Policy                {                    AirCode = "CA",                    Dep = "PEK,CAN,CTU",                    Arr = "PEK,CAN,CTU,SHA",                    Cabin = "D,M,Y"                });            }            Stopwatch sp = new Stopwatch();            sp.Start();            List<Policy> result = new List<Policy>();            list.ForEach(m =>            {                var s = (from d in m.Dep.Split(',')                         from a in m.Arr.Split(',')                         from c in m.Cabin.Split(',')                         where d != a                         select new Policy                         {                             AirCode = m.AirCode,                             Dep = d,                             Arr = a,                             Cabin = c                         }).ToList();                s.ForEach(n =>                {                    result.Add(n);                });            });            sp.Stop();            //result.ForEach(m =>            //{            //    Console.WriteLine(m.ToString());            //});            Console.WriteLine("count:{0},time:{1}", result.Distinct().Count(),sp.ElapsedMilliseconds);            Console.Read();        }    }    public class Policy    {        public string AirCode { get; set; }        public string Dep { get; set; }        public string Arr { get; set; }        public string Cabin { get; set; }        public override string ToString()        {            return string.Format("AirCode:{0},Dep:{1},Arr:{2},Cabin:{3}", AirCode, Dep, Arr, Cabin);        }    }}


0 0
原创粉丝点击