什么是LINQ

先短暂介绍linq,然后用代码演示。

什么是LINQ

语言集成查询(英语:Language Integrated Query,缩写:LINQ),发音"link",是微软的一项技术,新增一种自然查询的SQL语法到.NET Framework的编程语言中,当前可支持C#以及Visual Basic .NET语言。2007年11月19日随.NET Framework 3.5发布了LINQ技术。

包括LINQ to Objects、LINQ to SQL、LINQ to Datasets、LINQ to Entities、LINQ to Data Source、LINQ to XML/XSD等。

它也有标准查询运算符:

Where筛选操作符(Restriction)Where(Predicate)

Dim lowNums = numbers.Where(Function(num) num < 5)

 

Join连接操作符内连接两个或多个表,仅限于Equals运算符

from sup in suppliers join cust in customers on sup.Country equals cust.Country

 

OrderBy排序操作符(Ordering) 

Dim query As IEnumerable(Of Person) = Persons.OrderBy(Function(p) p.Age)

 

GroupBy分组操作符 Dim numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}
  Dim numberGroups = From num In numbers Group num By remainder5 = (num Mod 5) Into numGroup = Group Select New With {.Remainder = remainder5, .numbers = numGroup}


在C#2.0以前,如果要实现这样的功能,我们必须使用'foreach'或'for'循环来遍历数组,先找到偶数然后在降序排序,相关代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqOfSelectOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            // 查询出数组中的偶数并排序
            int[] ints = { 5, 2, 0, 66, 4, 32, 7, 1 };
            // 定义一个整数类型的集合,用来存放数组中的偶数
            List<int> list = new List<int>();
            // 遍历数组查询出偶数放到集合中
            foreach (int i in ints)
            {
                // 如果是偶数,把偶数加入到集合中
                if (i % 2 == 0)
                {
                    list.Add(i);
                }
            }

            // 正序排序
            list.Sort();
            // 反转
            list.Reverse();
            // 输出
            Console.WriteLine(string.Join(",",list));

            Console.ReadKey();
        }
    }
}

------------------------------------------------------------------------------------------------------------------------------------------------------------

 使用for循环很麻烦,而且不可维护和可读。C#2.0引入了delegate,可以使用委托来处理这种场景,代码如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqOfSelectOperation
{
    // 定义委托
    delegate bool FindEven(int item);

    class IntExtension
    {
        public static int[] where(int[] array, FindEven dele)
        {
            int[] result=new int[5];
            int i = 0;
            foreach (int item in array)
            {
                if (dele(item))
                {
                   result[i]=item;
                    i++;
                }
            }

            return result;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // 查询出数组中的偶数并排序
            int[] ints = { 5, 2, 0, 66, 4, 32, 7, 1 };

            //delegate(int item){return item % 2 == 0;}表示委托的实现
            List<int> list = IntExtension.where(ints, delegate(int item)
            {
                return item % 2 == 0;
            }).ToList();
            // 正序排序
            list.Sort();
            // 反转
            list.Reverse();
            // 输出
            Console.WriteLine(string.Join(",", list));

            Console.ReadKey();
        }
    }
}

------------------------------------------------------------------------------------------------------------------------------------------------------------

 所以,有了C#2.0,通过使用委托有了代理的优势,不必使用for循环来查询不同条件的数组。例如你可以使用相同的委托来查找数组中的奇数,并降序排序输出,代码如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqOfSelectOperation
{
    // 定义委托
    delegate bool FindEven(int item);

    class IntExtension
    {
        public static int[] where(int[] array, FindEven dele)
        {
            int[] result=new int[3];
            int i = 0;
            foreach (int item in array)
            {
                if (dele(item))
                {
                   result[i]=item;
                    i++;
                }
            }

            return result;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // 查询出数组中的奇数并排序
            int[] ints = { 5, 2, 0, 66, 4, 32, 7, 1 };

            //delegate(int item){return item % 2 != 0;}表示委托的实现
            List<int> list = IntExtension.where(ints, delegate(int item)
            {
                return item % 2 != 0;
            }).ToList();
            // 正序排序
            list.Sort();
            // 反转
            list.Reverse();
            // 输出
            Console.WriteLine(string.Join(",", list));

            Console.ReadKey();
        }
    }
}

------------------------------------------------------------------------------------------------------------------------------------------------------------

 虽然使用delegate可以使程序的可读性增加了,但是C#团队认为他们仍然需要使代码更加紧凑和可读,所以他们在C#3.0中引入了扩展方法、Lambda表达式、匿名类型等新特性,你可以使用C#3.0的这些新特性,这些新特性的使用LINQ的前提,可以用来查询不同类型的集合,并返回需要的结果。

下面的示例演示了如何使用LINQ和Lambda表达式根据特定条件来查询数组,示例代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqOfSelectOperation
{  
    class Program
    {
        static void Main(string[] args)
        {
            // 查询出数组中的奇数并排序
            int[] ints = { 5, 2, 0, 66, 4, 32, 7, 1 };

            // 使用LINQ和Lambda表达式查询数组中的偶数
            int[] intEvens= ints.Where(p => p % 2 == 0).ToArray();
            // 使用LINQ和Lambda表达式查询数组中的奇数
            int[] intOdds = ints.Where(p => p % 2 != 0).ToArray();

            // 输出
            Console.WriteLine("偶数:" + string.Join(",", intEvens));
            Console.WriteLine("奇数:" + string.Join(",", intOdds));

            Console.ReadKey();
        }
    }
}

------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vipers_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值