zoukankan      html  css  js  c++  java
  • C# Linq Group By 多个字段并返回给实体类List

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace study
     7 {
     8     class LinqGroup
     9     {
    10         static void Main(string[] args)
    11         {
    12             List<Employee> empList = new List<Employee>  
    13             {  
    14                new Employee {ID = 1, FName = "John", Age = 23, Sex = 'M', Money=100},  
    15                new Employee {ID = 2, FName = "Mary", Age = 25, Sex = 'F', Money=200},  
    16                new Employee {ID = 3, FName = "Amber", Age = 23, Sex = 'M', Money=300},  
    17                new Employee {ID = 4, FName = "Kathy", Age = 25, Sex = 'F', Money=400},  
    18                new Employee {ID = 5, FName = "Lena", Age = 27, Sex = 'F', Money=500},  
    19                new Employee {ID = 6, FName = "Bill", Age = 28, Sex = 'M', Money=600},  
    20                new Employee {ID = 7, FName = "Celina", Age = 27, Sex = 'F', Money=700},  
    21                new Employee {ID = 8, FName = "John", Age = 28, Sex = 'M', Money=800}   
    22             };
    23 
    24             List<Employee> empGroupByList = (from a in empList
    25                                              group a by new
    26                                              {
    27                                                  a.FName,
    28                                                  a.Sex
    29                                              } into b
    30                                              orderby b.Key.FName, b.Key.Sex
    31                                              select new Employee
    32                                              {
    33                                                  FName = b.Key.FName,
    34                                                  Sex=b.Key.Sex,
    35                                                  Age = b.Sum(c => c.Age),
    36                                                  ID = b.Max(c => c.ID),
    37                                                  Money = b.Average(c => c.Money)
    38                                              }).ToList<Employee>();
    39             foreach (var item in empGroupByList)
    40             {
    41                 Console.WriteLine("item.ID = " + item.ID);
    42                 Console.WriteLine("item.FName = " + item.FName);
    43                 Console.WriteLine("item.Age = " + item.Age);
    44                 Console.WriteLine("item.Sex = " + item.Sex);
    45                 Console.WriteLine("item.Money = " + item.Money);
    46                 Console.WriteLine("------------------------");
    47             }
    48 
    49             Console.ReadKey();
    50         }
    51     }
    52 
    53     public class Employee
    54     {
    55         public int ID { get; set; }
    56         public string FName { get; set; }
    57         public int Age { get; set; }
    58         public char Sex { get; set; }
    59         public decimal Money { get; set; }
    60     }
    61 }
    View Code

  • 相关阅读:
    echart------属性详细介绍
    网页链接(插件,判断服务)
    简单的轮播效果
    实时时间
    Oracle Partition By 的使用
    Java配置----JDK开发环境搭建及环境变量配置
    流程控制语句以及引号的使用
    解决报表表头格式问题
    k3could报表中替换最后行汇总字段方法
    k3cloud中使用委托添加提示对话框点击确定后执行方法
  • 原文地址:https://www.cnblogs.com/akatuki/p/3696598.html
Copyright © 2011-2022 走看看