Linq学习2

Posted ifconfig

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linq学习2相关的知识,希望对你有一定的参考价值。

参考书籍 《C# in a nutshell 8.0》

技术图片

基本数据单元是序列和元素,序列是任何实现了IEnumberable<T>接口的对象,而其中的每一项叫做一个元素。

Names就是一个序列,"Tom","Dick","Harry"就是元素。

Names 表示内存中的本地对象的集合,称之为"本地序列"

We call this a local sequence because it represents a local collection
of objects in memory.

查询运算符(query operator)是转换序列的方法。一个典型的查询运算符,接受一个输入序列,转换产生一个输出序列。

A query operator is a method that transforms a sequence. A typical
query operator accepts an input sequence and emits a transformed
output sequence.

在命名空间System.Linq中的Enumerable类,定义了约40种查询操作(query operators--所有都是以静态扩展方法的形式来实现的。

称为标准查询运算符。

In the Enumerable class in System.Linq,there are around 40 query operators—all implemented as static extension methods. These are called standard query operators.


NOTE
Queries that operate over local sequences are called local queries or LINQ-to-objects queries.
LINQ also supports sequences that can be dynamically fed from a remote data source
such as a SQL Server database. These sequences additionally implement the IQueryable<T> interface and are supported through a matching set of standard query operators in the Queryable class. We discuss this further in "Interpreted Queries"

?

?

?

///////////////////////////////////////////////////

?

A query is an expression that, when enumerated, transforms sequences with query operators. The simplest query comprises one
input sequence and one operator. For instance, we can apply the
Where operator on a simple array to extract those strings whose
length is at least four characters, as follows:

技术图片

?

?

using System;

using System.Collections.Generic;

using System.Linq;

?

namespace csharpnut_linq001

{

class Program

{

static void Main(string[] args)

{

string[] names = { "孙悟空", "猪八戒", "唐僧", "沙和尚", "白龙马" };

IEnumerable<string> filteredNames = Enumerable.Where(names, n => n.Length >= 3);

foreach (string name in filteredNames)

{

Console.WriteLine(name);

}

?

}

}

}

输出结果:

技术图片

?

改进一下:

技术图片

以上是关于Linq学习2的主要内容,如果未能解决你的问题,请参考以下文章

Linq学习之旅——LINQ查询表达式

LINQ学习——JOIN

LINQ学习——Group

LINQ编程技术内幕 通用部分 学习

LINQ 学习路程 -- 查询操作 ThenBy & ThenByDescending

Linq学习-初涉Linq