您的位置:首页 > 其它

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

2014-12-09 15:52 543 查看
效果:

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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐