LINQ -2015-04-27

LINQ--language-integrated-query

1、它和sql语言区别呢?

SQL语言常用在ralational-database中,而LINQ对内存数据,数据库,xml文件等多种形式进行操作。简而言之,操作范围广。

2、下面我们简单地来实现一下这个基本的linq:

首先,创一个customer类:

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

namespace LINQ_Exam
{
    //create a simple Customer class
    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string HomeAddress{ get; set; }
        //now override the ToString function of Object class.
        public override string ToString()
        {
            return string.Format("{0} {1} Enmail:{2}",FirstName,LastName,HomeAddress);
        }
        public static List<Customer> CreateCustomerList()
        {
            List<Customer> customers = new List<Customer>
            {
                 new Customer { FirstName = "Orlando",LastName = "Gee", HomeAddress = "orlando0@adventure-works.com"},
                 new Customer { FirstName = "Keith", LastName = "Harris",HomeAddress = "keith0@adventure-works.com" },
                 new Customer { FirstName = "Donna", LastName = "Carreras",HomeAddress = "donna0@adventure-works.com" },
                 new Customer { FirstName = "Janet", LastName = "Gates",HomeAddress = "janet1@adventure-works.com" },
                 new Customer { FirstName = "Lucy", LastName = "Harrington",HomeAddress = "lucy0@adventure-works.com" }
            };
            return customers;
        }
    }
}

然后,写几句话去用LINQ了,我在test类中main函数中去实现:

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

namespace LINQ_Exam
{
    public class Tester
    {
        public static void Main()    
        {
            List<Customer> customers = Customer.CreateCustomerList();
            IEnumerable<Customer> result = from customer in customers
                                           where customer.FirstName == "Donna"
                                           select customer;
            Console.WriteLine("FirstName = "Donna"");
            foreach(Customer customer in result)
            {
                Console.WriteLine(customer.ToString());
            }
            customers[3].FirstName = "Donna";
             Console.WriteLine("FirstName == "Donna" (take two)");
             foreach (Customer customer in result)
                {  Console.WriteLine(customer.ToString());}
          }
       }
}
然后执行结果为:

可见,linq好似和sql的用法差不多,只是要记住其范围可用更加广阔。

3、在这小例子中,我们需要注意的问题:

*

from   customer  in customers  这里的customer其实是一个变量名称,它迭代了customers里面的每一个数值,就相当于吧customers里面的

每一个值去赋予到customer中,然后进行筛选,为真的则保留下来,然后得到最后的查找结果。

*

Deferred(延期的) Query Evaluation

IEnumerable<Customer> result = from   customer  in customers

  where  customer.FirstName == "Donna“   select  customer;

这个是定义一个查找结果的代码,当代码写出来的时候,代码并没有运行产生一个result,此时仅仅是定义一个result的变量,这个变量里还没有任何数据。

因为下面的LINQ语句并没有执行。然而多会执行了呢?-------------------当result需要被用到的时候,就被执行了,就相当于result现场被填充了内容。

如:

foreach (Customer customer in result) {………}这个语句用到了result,然后result就会被立马填充内容了

这个很类似于sql中的view(视图)视图是空的,显示视图的内容只是进行一个对表的查找,然后再显示出来。就是这样啦。

ps:如果你不想让它这么干,可以这么写:(用到了ToList或者ToArray)

ToList<Customer> result = from   customer  in customers

  where  customer.FirstName == "Donna“   select  customer;

原文地址:https://www.cnblogs.com/yanwenxiong/p/4459668.html