zoukankan      html  css  js  c++  java
  • linq lambda GroupBy 用法

    Linq 中按照多个值进行分组(GroupBy)

     
    /// <summary>要查询的对象</summary>
    class Employee {
       public int ID { get;set; }
       public string FName { get; set; }
       public int Age { get; set; }
       public char Sex { get; set; }
    }

    如果对这个类的Age和Sex的连个字段进行分组,方法如下:

    // 先造一些数据
    List<Employee> empList = new List<Employee>();
    empList.Add(new Employee() {
       ID = 1, FName = "John", Age = 23, Sex = 'M'
    });
    empList.Add(new Employee() {
       ID = 2, FName = "Mary", Age = 25, Sex = 'F'
    });
    
    empList.Add(new Employee() {
       ID = 3, FName = "Amber", Age = 23, Sex = 'M'
    });
    empList.Add(new Employee() {
       ID = 4, FName = "Kathy", Age = 25, Sex = 'M'
    });
    empList.Add(new Employee() {
       ID = 5, FName = "Lena", Age = 27, Sex = 'F'
    });
    
    empList.Add(new Employee() {
       ID = 6, FName = "Bill", Age = 28, Sex = 'M'
    });
    
    empList.Add(new Employee() {
       ID = 7, FName = "Celina", Age = 27, Sex = 'F'
    });
    empList.Add(new Employee() {
       ID = 8, FName = "John", Age = 28, Sex = 'M'
    });

    接下来的做法是:

    // 实现多key分组的扩展函数版本
    var sums = empList
             .GroupBy(x => new { x.Age, x.Sex })
             .Select(group => new {
                Peo = group.Key, Count = group.Count()
             });
    foreach (var employee in sums) {
       Console.WriteLine(employee.Count + ": " + employee.Peo);
    }
    
    // 实现多key分组的lambda版本
    var sums2 = from emp in empList
                group emp by new { emp.Age, emp.Sex } into g
                select new { Peo = g.Key, Count = g.Count() };
    foreach (var employee in sums) {
       Console.WriteLine(employee.Count + ": " + employee.Peo);
    }

    这个例子中就充分利用了匿名类型。

    varListByOwner= list.GroupBy(l => l.Owner)
                             
    .Select(lg =>
                                   
    new{
                                       
    Owner= lg.Key,
                                       
    Boxes= lg.Count(),
                                       
    TotalWeight= lg.Sum(w => w.Weight),
                                       
    TotalVolume= lg.Sum(w => w.Volume)
                                   
    });

     var q =from b in listOfBoxes
                   
    group b by b.Ownerinto g
                   
    selectnew
                              
    {
                                  
    Owner= g.Key,
                                  
    Boxes= g.Count(),
                                  
    TotalWeight= g.Sum(item => item.Weight),
                                  
    TotalVolume= g.Sum(item => item.Volume)
                              
    };

  • 相关阅读:
    n9多媒体不显示图片处理方法
    STM32全球唯一ID读取方法
    VS2008+QT+CYAPI开发USB程序问题
    QT对话框中show和exec的区别
    华硕T20信号差的解决办法
    使用JTAG方式配置EPCS芯片时显示容量不够的解决方法
    QT中使用中文
    MODBUS CRC16
    递归的四条基本法则
    Java代码混淆和加密Jocky
  • 原文地址:https://www.cnblogs.com/yxlblogs/p/3222520.html
Copyright © 2011-2022 走看看