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

     

  • 相关阅读:
    android基础开发之一setContentView用法
    setContentView( )方法
    如何使用andriod的布局标签
    也谈layout_gravity和gravity的用法
    SlidingMenu第二篇 --- SlidingMenu常用属性介绍
    SlidingMenu第一篇 --- 导入SlidingMenu库
    JS----对象的合并与克隆
    angular2 ----字符串、对象、base64 之间的转换
    WebStorm过期解决方法
    JavaScript--鼠标滚动改变图片大小
  • 原文地址:https://www.cnblogs.com/han-guang-xue/p/10534301.html
Copyright © 2011-2022 走看看