• 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
        }
    }

     

  • 相关阅读:
    gridFS-Nginx的安装与使用
    centos下利用phantomjs来完成网站页面快照截图
    linux下安装php的svn模块
    在Thinkphp3.1中使用Mongo的具体操作
    CentOS 6.4安装mongo的php扩展包
    在centos6.3下安装php的Xdebug
    在yum安装lamp的环境下安装coreseek以及php的sphinx扩展
    CentOS 6.4下通过YUM快速安装配置LAMP服务器(Apache+PHP5+MySQL)
    微信中web页面实现和公众号中查看图片一样的效果
    ionic学习教程地址梳理
  • 原文地址:https://www.cnblogs.com/han-guang-xue/p/10534301.html
Copyright © 2020-2023  润新知