LINQ - 对象

LINQ to Objects 允许使用任何支持 IEnumerable<T> 的 LINQ 查询来访问内存数据集合,而无需 LINQ 提供程序 (API),就像 LINQ to SQL 或 LINQ to XML 的情况一样。

LINQ to Objects 简介

LINQ to Objects 中的查询通常仅返回 IEnumerable<T> 类型的变量。简而言之,LINQ to Objects 为集合提供了一种全新的方法,因为以前,编写长代码(非常复杂的 foreach 循环)来从集合中检索数据至关重要,而现在,这种方法已被编写声明性代码所取代,这些代码清楚地描述了需要检索的数据。

与传统的 foreach 循环相比,LINQ to Objects 还具有许多优势,例如可读性更强、过滤功能强大、分组能力强、以最少的应用程序编码增强了排序功能。此类 LINQ 查询本质上也更紧凑,无需任何修改或只需稍加修改即可移植到任何其他数据源。

下面是一个简单的 LINQ to Objects 示例 −

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 to Objects 查询的对象集合。

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

当上述代码被编译并执行时,它会产生以下结果 −

Tablesaw
Bandsaw
Planer
Jointer
Drill
Sander

使用 LINQ to Objects 查询内存集合

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("
Press 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.