zoukankan      html  css  js  c++  java
  • C#中linq查询机制

    //可在后面加上语句  .count();会立即执行,不会延迟执行

    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data;
    
    namespace Dome_2
    
    {
        class Program
        {
            static void Main(string[] args)
            {
                linqDome5();
               
            }
    
            #region linq查询机制 
    
            #region 
            public static void LinqDome6(){
    
            }
            #endregion
    
            #endregion
    
            #region linq 相关练习
    
            #region linq的方法对字符串的处理
            public static void linqDome6(){
                string[] nums = {"张三","李四","李达","查看及时答复","程思佳","非法","成吉思汗","我不是"};
    
                var list = nums.Where(x=>x.Length==2).Select(x=>x).GroupBy(x=>x.Substring(0,1));
    
                
    
                foreach(var num in list)
                {
                    Console.WriteLine("姓====>"+num.Key);
    
                    foreach(var x in num){
                        Console.WriteLine(x);
                    }
    
                }
            }
            #endregion
    
            #region linq的方法对字符串的处理和排序
            public static void linqDome5(){
                string[] nums = {"张三","李四","车嘎达","车嘎新","查看及时答复","程思佳打击非法","成吉思汗","我不是"};
    
                var list = nums.Where(x=>x.Length==3).Select(x=>x).GroupBy(x=>x.Substring(0,1));
    
                foreach(var num in list)
                {
                    Console.WriteLine("姓====>"+num.Key);
    
                    foreach(var x in num){
                        Console.WriteLine(x);
                    }
    
                }
            }
            #endregion
    
            #region linq的方法OrderBy排序
            public static void linqDome4(){
                int[] nums = {1,88,6,7,2,6,5,4,9,13,20};
    
                //var list = nums.Where(x=>x%2==0).Select(i=>i*i).OrderByDescending(x=>x);  降序
    
                var list = nums.Where(x=>x%2==0).Select(i=>i*i).OrderBy(x=>x);      //升序
    
                foreach(var num in list)
                {
                    Console.WriteLine(num);
                }
            }
            #endregion
    
            #region linq的方法where
            public static void linqDome3(){
                int[] nums = {1,7,2,6,5,4,9,13,20};
    
                var list = nums.Where(x=>x%2==0);
    
                foreach(var num in list)
                {
                    Console.WriteLine(num);
                }
            }
            #endregion
    
            #region linq的方法select求数组每个数的平方
            public static void linqDome2(){
                int[] nums = {1,7,2,6,5,4,9,13,20};
    
                var list = nums.Select(x=>x*x);
    
                foreach(var num in list)
                {
                    Console.WriteLine(num);
                }
            }
            #endregion
    
            #region Linq的基础练习(初识)
            public static void LinqDome(){
                 int[] nums = {1,7,2,6,5,4,9,13,20};
    
                var list = from num in nums where num%2!=0 orderby num descending select num; 
                
                foreach(var num in list)
                {
                    Console.WriteLine(num);
                }
    
                Console.WriteLine("Hello World!");
            }
    
            #endregion
    
            #endregion
        }
    }

     

  • 相关阅读:
    Linux 操作memcache命令行
    查看memcache版本
    磊哥测评之数据库SaaS篇:腾讯云控制台、DMC和小程序
    一看就能学会的H5视频推流方案
    JavaScript与WebAssembly进行比较
    Android调试神器stetho使用详解和改造
    5分钟入门git模式开发
    深耕品质,腾讯WeTest《2018中国移动游戏质量白皮书》正式发布
    RSA签名的PSS模式
    附实例!图解React的生命周期及执行顺序
  • 原文地址:https://www.cnblogs.com/han-guang-xue/p/10534301.html
Copyright © 2011-2022 走看看