LINQ对象 - LinQ教程

LINQ到Objects提供任何LINQ查询支持的IEnumerable<T>访问内存中的数据集合,而不需要任何LINQ提供程序(API)的情况下,使用LINQ到SQL或LINQ到XML。

LINQ到对象简介

查询在LINQ到对象返回IEnumerable<T>只有类型的变量。 总之,LINQ到Objects在早期提供了一种新方法到集合,它需要写很长代码换成声明性代码的集合,清楚地描述了所需的数据编码(foreach循环复杂得多)进行数据检索至所需关键的检索。

LINQ有许多优势超过传统的foreach循环,更易读,强大的过滤,分组的能力,增强排序以最小的应用程序的编码对象。这样LINQ查询在性质上也更加紧凑,并且移植到任何其它数据源没有任何修改或只需稍加修改。

下面是一个简单的LINQ到对象的例子:

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

namespace LINQtoObjects
{
  class Program
  {
     static void Main(string[] args)
     {
        string[] tools = { "Tablesaw", "Bandsaw", "Planer", "Jointer", "Drill", 
                         "Sander" };
        var list = from t in tools
                   select t;

        StringBuilder sb = new StringBuilder();

        foreach (string s in list)
        {
           sb.Append(s + Environment.NewLine);
        }
        Console.WriteLine(sb.ToString(), "Tools");
        Console.ReadLine();
     }
  }
}

在这个例子中,字符串(工具)的阵列被用作对象的集合,使用LINQ到对象进行查询。

Objects query is:
var list = from t in tools
           select t;

当上述代码被编译和执行时,它产生了以下结果:

Tablesaw
Bandsaw
Planer
Jointer
Drill
Sander

使用LINQ到内存中的对象集合查询

C

using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQtoObjects
{
  class Department
  {
     public int DepartmentId { get; set; }
     public string Name { get; set; }
  }

  class LinqToObjects
  {
     static void Main(string[] args)
     {
        List<Department> departments = new List<Department>();
        departments.Add(new Department { DepartmentId = 1, Name = "Account" });
        departments.Add(new Department { DepartmentId = 2, Name = "Sales" });
        departments.Add(new Department { DepartmentId = 3, Name = "Marketing" });

        var departmentList = from d in departments
                             select d;

        foreach (var dept in departmentList)
        {
           Console.WriteLine("Department Id = {0} , Department Name = {1}",
                             dept.DepartmentId, dept.Name);
        }
        Console.WriteLine("\nPress any key to continue.");
        Console.ReadKey();
     }
  }
}

VB

Imports System.Collections.Generic
Imports System.Linq

Module Module1
  Sub Main(ByVal args As String())

     Dim account As New Department With {.Name = "Account", .DepartmentId = 1}
     Dim sales As New Department With {.Name = "Sales", .DepartmentId = 2}
     Dim marketing As New Department With {.Name = "Marketing", .DepartmentId = 3}

     Dim departments As New System.Collections.Generic.List(Of Department)(New Department() {account, sales, marketing})

     Dim departmentList = From d In departments

     For Each dept In departmentList
        Console.WriteLine("Department Id = {0} , Department Name = {1}", dept.DepartmentId, dept.Name)
     Next

     Console.WriteLine(vbLf & "Press any key to continue.")
     Console.ReadKey()
  End Sub

  Class Department
     Public Property Name As String
     Public Property DepartmentId As Integer
  End Class
End Module

当C#或VB的上述代码被编译和执行时,它产生了以下结果:

Department Id = 1, Department Name = Account
Department Id = 2, Department Name = Sales
Department Id = 3, Department Name = Marketing

Press any key to continue.