List集合实战总结
分割List集合

//构造被分隔的集合 List<object> list = new List<object>(); for (int i = 0; i <= 100; i++) { list.Add(i); } //待导入集合组 List<IEnumerable<object>> BigList = new List<IEnumerable<object>>(); //定义批次分组提交量 int groupItemCount = 10; //被分隔集合的总数量 int totalCount = list.Count(); //一共被分几组 int count = totalCount % groupItemCount == 0 ? totalCount / groupItemCount : totalCount / groupItemCount + 1; //将每一组都添加进大集合 for (int i = 0; i < count; i++) { BigList.Add(list.Skip(groupItemCount * i).Take(groupItemCount)); } foreach (IEnumerable<object> enumList in BigList) { //每一个enumList都一个10个int数据的集合 foreach (int a in enumList) { //dosomething } }
public static IList<List<T>> ListCut<T>(IList<T> list, int everyCount) { //待导入集合组 IList<List<T>> BigList = new List<List<T>>(); //被分隔集合的总数量 int totalListCount = list.Count(); //一共被分几组 int count = totalListCount % everyCount == 0 ? totalListCount / everyCount : totalListCount / everyCount + 1; //将每一组都添加进大集合 for (int i = 0; i < count; i++) { BigList.Add(list.Skip(everyCount * i).Take(everyCount).ToList()); } return BigList; }
从string[]转list<string> string[] str={"1","2"}; list<string> list=new list<string>(str); 从list<string>转string[] list<string> list=new list<string>; string[] str=list.toarray();
去除数组中的 null
tableHeadNameArr = (from str in tableHeadNameArr where str != null select str).ToArray();
Select(取list中的id列数据,并按逗号分隔成字符串。例:1,2,3,4,5)

//方式一 //分成key-value的数组 string[] id = list.Select(a => a.id.ToString()).ToArray(); //dt是datatable类型的,执行LINQ语句,这里的.AsEnumerable()是延迟发生,不会立即执行,实际上什么都没有发生 string[] id = dt.AsEnumerable().Select(a => a.Field<int>("id").ToString()).ToArray(); //将数组变成1,2,3,4,5的字符串 string ids = string.Join(",", id); //方式二 //效果等同于foreach循环 foreach (var i in list) { ids += i.id + ","; } ids = ids.TrimEnd(','); 上述代码使用LINQ 针对数据集中的数据进行筛选和整理,同样能够以一种面向对象的思想进行数据集中数据的筛选。 在使用LINQ 进行数据集操作时,LINQ 不能直接从数据集对象中查询,因为数据集对象不支持LINQ 查询,所以需要使用AsEnumerable 方法返回一个泛型的对象以支持LINQ的查询操作。 .AsEnumerable()与相对应的.AsQueryable()的区别: 1) AsEnumerable()是 LINQ TO OBJECT,AsQueryable()是 LINQ TO SQL 2) AsEnumerable将一个序列向上转换为一个IEnumerable, 强制将Enumerable类下面的查询操作符绑定到后续的子查询当中。AsQueryable将一个序列向下转换为一个IQueryable, 它生成了一个本地查询的IQueryable包装。 3) AsEnumerable()延迟执行,不会立即执行。当你调用.AsEnumerable()的时候,实际上什么都没有发生。当真正使用对象的时候(例如调用:First, Single, ToList....的时候)才执行。 4) .ToList()立即执行
Where与Select的同时使用,取list中的id列数据,并按逗号分隔成字符串。
string[] id = list.Where(a => !string.IsNullOrEmpty(a.user_type)).Select(a => a.id).ToArray(); //ids="1,2,3,4,5,6,7"; string ids = string.Join(",", id);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!