zoukankan      html  css  js  c++  java
  • linq中的分组和排序

    一、分组  group 组内成员 by 分组条件 into 组的信息

     class Program
        {
            static void Main(string[] args)
            {
                string[] name = { "张三","张六","刘大","刘晓","刘大脑袋","王大锤"};
                var result = from n in name
                             group n by n[0] into g
                             select g;
                foreach (var g in result)
                {
                    Console.WriteLine(g.Key+""+g.Count()+"");
                    foreach (var item in g)
                    {
                        Console.WriteLine(item);
                    }
                }
            }
        }

    二、order by 排序

     class Program
        {
            static void Main(string[] args)
            {
                //降序排序,默认是升序
                List<int> nums = new List<int> { 12,46,55,87,45,12,12,1,2,3};
                //方法1 linq语句+linq方法
                var result = from i in nums
                             orderby i descending
                             select i;
                foreach (var i in result)
                {
                    Console.WriteLine(i);
                }
                //linq方法
                var num= nums.OrderByDescending(i => i);
                foreach (var i in num)
                {
                    Console.WriteLine(i);
                }
    
            }
        }

  • 相关阅读:
    Git 基本操作
    Git 基础
    MarkDown教程
    Python常用函数
    Python生成器
    Python列表生成式
    Python迭代
    Python切片
    Python函数
    Python不可变对象
  • 原文地址:https://www.cnblogs.com/ItDotNet/p/4981132.html
Copyright © 2011-2022 走看看