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

2015-01-23 21:53:52 · 作者: · 浏览: 12

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

比如,我们要实现从numbers数组中提取偶数并降序排列,在没有linq语句之前,我们的代码是这么来写的:

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

运行效果如下:

\

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

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);

            }
        }
    }
}

运行效果如下:

\

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

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

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