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);
}
}
}
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);
}
}
}
相关文章推荐
- System.Collections.Generic.List<>是System.Collections.ArrayList的泛型实现
- 无法将类型“System.Collections.Generic.List<anonymous type:string ClassID,string ClsssName>”隐式转换为“System.Collections.Generic.List<Ecology.Model.EnergyFlowGraph>”
- “System.Collections.Generic.IEnumerable<TestDAL.HistoryData>”不包含“ToList”的定义
- 无法将类型“System.Collections.Generic.IEnumerable<EmailSystem.Model.TemplateInfo>”隐式转换为“System.Collections.Generic.List<EmailSystem.Model.TemplateInf
- MVC 无法将类型“System.Collections.Generic.List<AnonymousType#1>”隐式转换为“System.Collections.Generic.IList<Mvc3Modeltest.Models.Movie>”。存在一个显式转换(是否缺少强制转换?))
- “System.Collections.Generic.IEnumerable<TestDAL.HistoryData>”不包含“ToList”的定义
- NHibernate无法将类型“System.Collections.Generic.IList<T>”隐式转换为“System.Collections.Generic.IList<IT>
- System.Collections.Generic.List<T> 与 System.Collections.ArrayList
- 从头认识Spring-1.16 SpEl对集合的操作(1)-建立集合以及访问集合的元素,以<util:list/>为例
- 从头认识Spring-1.16 SpEl对集合的操作(1)-建立集合以及訪问集合的元素,以<util:list/>为例
- System.Collections.Generic.IEnumerable<T>接口
- [corefx注释说]-System.Collections.Generic.Stack<T>
- System.Collection.IList 转换成为 System.Collection.Generic.IList<T>
- List<string>里 每个元素重复了多少次
- List<t>中如何将指定元素的值放到第一位
- 使用泛型类型System.Collections.Generic.Icomparer &lt;T&gt; 需要一个类型参数
- 一个提醒...such as implement System.Collections.Generic.IComparer<T>
- [corefx注释说]-System.Collections.Generic.Queue<T>
- 使用泛型 类型“System.Collections.Generic.IEnumerator<T>”需要 1 个类型参数
- 加强型无穷集合:InfiniteList<T>,可指定遍历方向和偏移量,只要集合有元素并且偏移量不为 0,将永远遍历下去。