Linq查询案例


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,如需转载请自行联系原作者
上一篇:Deepin字体下载与安装


下一篇:Oracle使用JDBC进行增删改查