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;
        }
    }
}
本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6849594.html,如需转载请自行联系原作者
相关文章
|
7月前
|
SQL 开发框架 .NET
C#进阶-LINQ实现对集合的增删改查
本篇演示了LINQ在日常开发中的常用操作,实现结果集的增删改查。目前LINQ支持两种语法,我会在每个案例前先用大家熟知的SQL语句表达,再在后面用C#的两种LINQ语法分别实现。LINQ语法第一次接触难免感到陌生,最好的学习方式就是在项目中多去使用,相信会有很多感悟。
97 0
|
开发框架 .NET 大数据
C#使用linq查询大数据集的方法
这篇文章主要介绍了C#使用linq查询大数据集的方法,涉及C#调用linq进行数据查询的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
2177 0
|
SQL .NET C#
用LINQ在集合中查询特定对象
这里是原文出处: 简单的概括LINQ LINQ是Language-Integrated Query的缩写,是C# 3.0和VB 9.0中新加入的语言特性,可以在编程时使用内置的查询语言进行基于集合的操作。
858 0
|
.NET 开发框架 存储
|
.NET 开发框架 数据格式