zoukankan      html  css  js  c++  java
  • Linq查询案例

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LinqDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] names = { "Alonso","Zheng","Small","Smile"};
                var queryResults = from n in names where n.StartsWith("S") select n; // n代表某一个元素,where指定查询的条件,select指定包含的元素
                Console.WriteLine("Names beginning with S:");
                foreach (var item in queryResults)
                {
                    Console.WriteLine(item);
                }
    
                Console.ReadLine();
            }
        }
    }
    
    
    

    改造后,功能一样

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LinqDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] names = { "Alonso","Zheng","Small","Smile","Ruiz","Singh"};
                // var queryResults = from n in names where n.StartsWith("S") select n; // n代表某一个元素,where指定查询的条件,select指定包含的元素
                var queryResults = names.Where(n => n.StartsWith("S"));
                Console.WriteLine("Names beginning with S:");
                foreach (var item in queryResults)
                {
                    Console.WriteLine(item);
                }
    
                Console.ReadLine();
            }
        }
    }
    
    

    增加排序

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LinqDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] names = { "Alonso","Zheng","Small","Smile","Ruiz","Singh"};
                var queryResults = from n 
                                   in names
                                   where n.StartsWith("S")
                                   orderby n
                                   select n; // n代表某一个元素,where指定查询的条件,select指定包含的元素
                // var queryResults = names.Where(n => n.StartsWith("S"));
                Console.WriteLine("Names beginning with S:");
                foreach (var item in queryResults)
                {
                    Console.WriteLine(item);
                }
    
                Console.ReadLine();
            }
        }
    }
    
    

    降序排列

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LinqDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] names = { "Alonso","Zheng","Small","Smile","Ruiz","Singh"};
                var queryResults = from n 
                                   in names
                                   where n.StartsWith("S")
                                   orderby n descending // Z-A将序
                                   select n; // n代表某一个元素,where指定查询的条件,select指定包含的元素
                // var queryResults = names.Where(n => n.StartsWith("S"));
                Console.WriteLine("Names beginning with S:");
                foreach (var item in queryResults)
                {
                    Console.WriteLine(item);
                }
    
                Console.ReadLine();
            }
        }
    }
    
    

    按照最后一个字母排序

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LinqDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] names = { "Alonso","Zheng","Small","Smile","Ruiz","Singh"};
                var queryResults = from n 
                                   in names
                                   where n.StartsWith("S")
                                   orderby n.Substring(n.Length -1) // 按最后一个字母排序
                                   select n; // n代表某一个元素,where指定查询的条件,select指定包含的元素
                // var queryResults = names.Where(n => n.StartsWith("S"));
                Console.WriteLine("Names beginning with S:");
                foreach (var item in queryResults)
                {
                    Console.WriteLine(item);
                }
    
                Console.ReadLine();
            }
        }
    }
    
    

    通过OrderBy方法排序

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LinqDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] names = { "Alonso","Zheng","Small","Smile","Ruiz","Singh"};
                //var queryResults = from n 
                //                   in names
                //                   where n.StartsWith("S")
                //                   orderby n.Substring(n.Length -1) // 按最后一个字母排序
                //                   select n; // n代表某一个元素,where指定查询的条件,select指定包含的元素
                var queryResults = names.OrderBy(n => n).Where(n => n.StartsWith("S")); // 通过OrderBy方法排序
                Console.WriteLine("Names beginning with S:");
                foreach (var item in queryResults)
                {
                    Console.WriteLine(item);
                }
    
                Console.ReadLine();
            }
        }
    }
    
    

    大数据查询

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LinqDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] numbers = GenerateLotsOfNumbers(123456789);
                var queryResults = from n in numbers
                                   where n < 1000
                                   select n;
                Console.WriteLine("小于1000的数字:");
                foreach (var item in queryResults)
                {
                    Console.WriteLine(item);
                }
    
                Console.ReadLine();
            }
    
            // 随机数列表
            private static int[] GenerateLotsOfNumbers(int count)
            {
                Random generator = new Random(0);
                int[] result = new int[count];
                for (int i = 0; i< count; i++)
                {
                    result[i] = generator.Next();
                }
                return result;
            }
        }
    }
    
    
  • 相关阅读:
    Redis系列(八)--缓存穿透、雪崩、更新策略
    Vue在单独引入js文件中使用ElementUI的组件
    解读浮动闭合最佳方案:clearfix
    JS replace()方法替换变量(可以对变量进行全文替换)
    Django media的设置
    使用EventBus实现兄弟组件之间的通信
    component: resolve => require(['../pages/home.vue'], resolve)
    vue中使用localStorage存储信息
    使用vue-router beforEach实现判断用户登录跳转路由筛选功能
    ES6使用常量做为函数名
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/6849594.html
Copyright © 2011-2022 走看看