1.直接贴代码块List的扩展
public static class ListExtension
{
/// <summary>
/// 分页数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">数据源</param>
/// <param name="batchSize">分页大小</param>
/// <returns></returns>
public static IEnumerable<IEnumerable<T>> Batch<T>(this List<T> source, int batchSize)
{
if (source == null || batchSize <= 0) yield break;
for (int i = 0; i < source.Count; i += batchSize)
{
int remaining = source.Count - i;
int currentBatchSize = Math.Min(remaining, batchSize);
var batch = source.Skip(i).Take(currentBatchSize);
yield return batch;
}
}
}
2.完整示例
class Program
{
static void Main(string[] args)
{
//定义测试数据
List<MyData> data = new List<MyData>()
{
new MyData(){ Index = 0, Name ="张三" },
new MyData(){ Index = 1, Name ="李四" },
new MyData(){ Index = 2, Name ="王五" },
new MyData(){ Index = 3, Name ="赵六" },
};
//定义梳理
int batchSize = 3;
//扩展调用
var lis2 = data.Batch(batchSize);
foreach (var item in lis2)
{
Console.WriteLine(JsonConvert.SerializeObject(item));
}
}
/// <summary>
/// 测试数据的Data
/// </summary>
public class MyData
{
public int Index { get; set; }
public string Name { get; set; }
}
}
结果:
完成了,好了处理速度还是比较快的
标签:Index,Name,C#,List,MyData,数据处理,new,public From: https://www.cnblogs.com/geekX/p/18040512