List<Model> strList = new List<Model>() { new Model(){Id=1,Name = "张三"}, new Model(){Id=11,Name = "张三1"}, new Model(){Id=2,Name = "李四"}, new Model(){Id=3,Name = "王五"}, new Model(){Id=30,Name = "王五"}, new Model(){Id=4,Name = "赵六"}, }; //查询List对象某个字段 var names = strList.Select(t => t.Name).ToList(); //字段值合并 string mergeName = string.Join("|", names.ToArray());
1 2 3 4 5 6 | //根据某个字段分组 var types = strList.GroupBy(x => x.Name) .Select( group => new Model { Name = group .Key, }).ToList(); |
//List中实现类似SQL 查询 select in...
1 2 3 4 5 6 7 8 | List< int > whereList = new List< int >() { 1, 3 }; List<Model> list = strList.Where(x => whereList.Contains(x.Id)).ToList(); foreach ( var model in list) { Console.WriteLine( "Id:" + model.Id + "Name:" + model.Name); } Console.WriteLine( "list count==" + list.Count); |
1 2 3 4 | //更改list中某个字段 list.ForEach(o => o.Age = 33); list.All(o => { o.Age = 22; return true ; }); |
1 2 | //List中获取最大值 int maxA = list. Max (x => x.A);//获取链表中对象属性A的最大值 |
数组、list转化
1 2 3 4 5 6 7 8 | List< string > listS= new List< string >(); listS.Add( "str" ); listS.Add( "hello" ); string [] str=listS.ToArray(); string [] stauts = dict[0].SDValue.Split( '|' ); List< string > tempStatus = new List< string >(stauts); where = where .And(o => tempStatus.Contains(o.STATUS)); |
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2019-03-21 VS2012 统计代码行数
2018-03-21 自定义控件添加事件