lambda表达式 巧用

首先定义一个学生成绩类

public class StudentResult
{
    public int Id { get; set; }
    public string No { get; set; }
    public string Name { get; set; }
    public int Num { get; set; }
}

1、分页

public ClassA()
{
    int pageIndex = 1;int pageSize = 100;
    var tempList = new List();
    var lastList = tempList.Skip((pageIndex-1)*pageIndex).Take(pageIndex).ToList();
}

2、聚合计算

public List GetGroupSum(List list)
{
    var tempList = list.GroupBy(v => new { v.Id, v.No, v.Name }).Select(o => new { keys = o.Key, tempNum = o.Sum(v => v.Num) }).ToList();
    var lastList = new List();
    tempList.ForEach(v =>
    {
        lastList.Add(new StudentResult() { Id = v.keys.Id, No = v.keys.No, Name = v.keys.Name,Num = v.tempNum });
    });
    return lastList;
}

你可能感兴趣的:(lambda表达式 巧用)