博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
List泛型类的方法及使用
阅读量:7186 次
发布时间:2019-06-29

本文共 1211 字,大约阅读时间需要 4 分钟。

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;
}
}

转载于:https://www.cnblogs.com/liuslayer/p/4981603.html

你可能感兴趣的文章
Matlab查看数值不用科学计数法显示
查看>>
C# 读取资源文件.resx 中的xml资源
查看>>
python版mapreduce题目实现寻找共同好友
查看>>
提高mysql千万级大数据SQL查询优化30条经验(Mysql索引优化注意)
查看>>
前端性能优化(css动画篇)
查看>>
用户体验评价
查看>>
[SCOI2012]滑雪与时间胶囊
查看>>
phonegap ios开发环境搭建
查看>>
NOIP2003 传染病控制
查看>>
【java】深入分析Java ClassLoader原理
查看>>
c# 自定义事件,实现变量的值改变后就触发该事件
查看>>
AMD OpenCL大学教程(8)
查看>>
【转】实现运动的尾巴效果
查看>>
leetcode Permutations II 无重全排列
查看>>
微信开发好的地址
查看>>
Linux菜鸟级重点
查看>>
字节排序问题
查看>>
python3读取chrome浏览器cookies
查看>>
android 网络编程 HttpGet类和HttpPost类使用详解
查看>>
添加图片后xcode报错:resource fork, Finder information, or similar detritus not allowed
查看>>