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,如需转载请自行联系原作者
相关文章
|
开发框架 .NET 大数据
C#使用linq查询大数据集的方法
这篇文章主要介绍了C#使用linq查询大数据集的方法,涉及C#调用linq进行数据查询的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
2169 0
|
.NET 开发框架 存储
|
.NET 开发框架 数据格式
|
.NET 索引 开发框架