List泛型类的方法及使用
static void Main(string[] args)
{
List<Customer> custs = new List<Customer>();
custs.Add(new Customer() { ID = 1 });
custs.Add(new Customer() { ID = 5 });
custs.First(new Func<Customer, bool>(delegate(Customer x) { return x.ID == 5; }));
custs.First(new Func<Customer, bool>((Customer x) => x.ID == 5));
custs.First(delegate(Customer x) { return x.ID == 5; });
custs.First((Customer x) => x.ID == 5);
custs.First(x => x.ID == 5);
custs.First(Customer.Test);
List<Customer> newCusts = custs.Where(x => x.ID == 5).ToList();//找到集合中满足指定条件的元素
int sum = custs.Sum(x => x.ID);//计算和
List<Customer> ofType = custs.OfType<Customer>().ToList();//根据指定类型,筛选集合中的元素
List<int> select = custs.Select(x => x.ID + 1).ToList();//将集合中的每个元素投影的新集合中
List<string> selectMany = custs.SelectMany(x => new List<string>() { "id:", x.ID.ToString() }).ToList();//将序列的每个元素投影到一个序列中,最终把所有的序列合并
List<ulong> select= custs.ConvertAll(item => Convert.ToUInt64(item));
//LINQ
sum= custs.Where(x => x.ID != 10).Sum(x => x.ID);
sum = (from v in custs where v.ID != 1 select v).Sum(x => x.ID);
}
class Customer
{
public int ID { get; set; }
public static bool Test(Customer x)
{
return x.ID == 5;
}
}
posted on 2015-11-20 17:42 liuslayer 阅读(1224) 评论(0) 编辑 收藏 举报