linq---我为你提笔序,你的美不只查询语句

        LinQ百度百科对她这样解释,是一组用于c#和Visual Basic语言的扩展。它允许编写C#或者Visual Basic代码以查询数据库相同的方式操作内存数据。 LINQ是Language Integrated Query的简称,翻译成中文就是语言集成查询,它是集成在.NET编程语言中的一种特性。已成为编程语言的一个组成部分,在编写程序时可以得到很好的编译时语法检查,丰富的元数据,智能感知、静态类型等强类型语言的好处。并且它同时还使得查询可以方便地对内存中的信息进行查询而不仅仅只是外部数据源。她提供多种查询方法,有select,where,orderby,groupby,小伙伴们是不是觉得在哪儿见过nie,哦,在梦里,开玩笑,跟我们之前学习过的sql相似,那么她们有什么区别nie,接着小编举例说明。

       比如,我们要实现从numbers数组中获取大于50的数,我们的代码是这么来写的:

       

<span style="font-size:18px;"><span style="font-size:18px;">using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ch01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            int[] arr = { 123, 12, 3, 12, 15, 4, 6, 46, 48, 56, 785 };
            //获取大于50的数
            //没有LinQ我们怎么做?
            ArrayList result = new ArrayList();
            for (int i = 0; i < arr.Length;i++ )
            {
                if (arr[i]> 50)
                {
                    result.Add(arr[i]);
                }
            }
            //打印result就ok了
            for (int i = 0;i<result.Count;i++)
            {
                Console.WriteLine(result[i]);
            }
        }
    }
}
</span></span>

         运行效果如下:

          

          接着,我们来看,如果使用linq,我们的代码又该如何写nie:

<span style="font-size:18px;">using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ch01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            int[] arr = { 123, 12, 3, 12, 15, 4, 6, 46, 48, 56, 785 };
           
            //有了LinQ后我们可以这么做
            //获取大于50的数
            IEnumerable ie = arr.Select(p => p).Where(p => p > 50);
            //输出--该部分代码可重用
            IEnumerator result = ie.GetEnumerator();
            while (result.MoveNext())
            {
                Console.WriteLine(result.Current);

            }
        }
    }
}
</span>

        运行效果如下:

        

        从上面的对比我们很容易可以看出,使用linq更加简洁,接下来,小编简单的介绍一下linq的扩展方法,那么什么是扩展方法呢?扩展方法又是如何出现的呢? .net framework为编程人员提供了很多的类,很多的方法,但是,不论.net framework在类中为我们提供了多么多的方法,有时候仍然不能满足我们的需求,例如:你想让字符串对象具有ToPascal方法,含义就是将字符串转化为Pascal格式,并返回,我们知道,.net framework提供的String类中并没有为我们提供相应的方法,此时,我们应该怎么做才可以达到我们的目的呢?有人说可以继承String类,这个不行,因为.net framework提供的类都是finnal类,最终类,不能被继承,那么,怎么样才可以解决这个问题呢?此时,就出现了扩展方法。扩展方法有哪些好处呢? 为现有类提供一些额外的方法,这样做的好处就是,原有类不需要重新编译生成,只需要在程序中引入一些额外的dll就可以了,我们来看一个具体的例子。

        比如说,我们有个字符串,当我们定义一个变量之后,比如说,我们给他一个值,在s字符串里面有一组方法,那么这组方法是系统帮我们定义好的,如果有天,我想要一个特殊的方法,比如topuuer转换成大写,tolower转换成小写,在接着,我想要首字母大写后面都小写,怎么办,这里面没有这种方法,怎么办nie,怎么去实现这种功能,这个就是我们扩展方法中要解决的问题,扩展方法的目标就是对现有的类提供额外的方法以增强该类的功能,我们可以这么来做,给他加一个扩展方法,首先,我们来做一个静态类,扩展方法是一种特殊的静态方法,我们怎么样来实现呢,如下:

<span style="font-size:18px;">using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ch01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

   private void btnExtraMethod_Click(object sender, EventArgs e)
        {
            //拓展方法,目标,对现有的类提供额外的方法以增强类的功能
            string s = "asEdSadaDsdSsa";
            Console.WriteLine(s.ToUpper());
            Console.WriteLine(s.ToLower());
            Console.WriteLine(s.ToPascal());
            Console.WriteLine(s.ToPascal(2));
        }
  //将字符串的手写字母转换为大写字母的方法
        public string toPascal(string s)
        {
            return s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower();
        }</span>
       运行效果如下:

       

       最后,小编来简单的介绍一下lambda表达式,C#Lambda基本的表达形式:(参数列表) => {方法体},说明一下,参数列表中的参数类型可以是明确类型或者是推断类型,如果是推断类型,则参数的数据类型将由编译器根据上下文自动推断出来,Lambda 用在基于方法的 LINQ 查询中,作为诸如Where 和 Where 等标准查询运算符方法的参数。我们来看一个具体的例子:

<span style="font-size:18px;">using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

public partial class Form1 : Form  
    {  
        //定义一个委托  
        public delegate string deleTransfer(string s);  
   
        public Form1()  
        {  
            InitializeComponent();  
        }  
   
        private void btnTest_Click(object sender, EventArgs e)  
        {  
            //拓展方法----  
            string strTest = "asdsad";  
            Console.WriteLine(strTest.ToLower());  
            Console.WriteLine(strTest.ToUpper());  
            Console.WriteLine(strTest.ToPascal());  
            Console.WriteLine("-------------------------------------");  
   
            //Lambda 来源  
            //.Net FrameWork 1.0委托---函数指针  
            deleTransfer trans = new deleTransfer(ToPascal);  //委托指向方法ToPascal  
            Console.WriteLine(trans("abcdEFGH"));  
   
            //.net 2.0 匿名方法  
            deleTransfer transs = delegate(string s) { return s.Substring(0,1).ToUpper() + s.Substring(1).ToLower(); };  
            Console.WriteLine(transs("abcdEFGH"));  
   
            //.net 3.5 匿名方法  
            //deleTransfertransss = (s) => (s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower());  
            deleTransfer transss = s =>s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower();  
            Console.WriteLine(transss("abcdEFGH"));  
        }  
   
        //将字符串的首字母转化为大写字母的方法  
        public string ToPascal(string s)  
        {  
            return s.Substring(0,1).ToUpper() + s.Substring(1).ToLower();  
        }  
    }  
   
    public static class ExtraClass  
    {  
        //拓展方法,特殊的静态方法  
        public static string ToPascal(this string s)   //this后带类型,表名为该类型添加特殊的方法  
        {  
            return s.Substring(0,1).ToUpper() + s.Substring(1).ToLower();  
        }  
        public static string ToPascal(this string s, int len)   //this后带类型,表名为该类型添加特殊的方法  
        {  
            return s.Substring(0,1).ToUpper() + s.Substring(1, len).ToLower() + s.Substring(len + 1);  
        }  
}  
</span>
       它的发展过程是在C#1.0中使用的委托,C#2.0中添加了匿名方法,C#3.0中添加了lambda表达式。

          小编寄语:该博文,小编主要简单的介绍了linq的一些基本知识,包括linq和sql的对比,linq的扩展方法以及lambda表达式,对linq的理解还不是很深刻,欢迎小伙伴们一起讨论交流,Linq的一个重要功能就是ORMapping思想的具体实现,ORMapping的思想就是实现编程的真正面向对象,使我们不用关心数据库,只需要关系实体类或实体集合类就行。项目进行中,未完待续......

评论 37
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值