王朝百科
分享
 
 
 

投影运算

王朝百科·作者佚名  2010-04-19  
宽屏版  字体: |||超大  

投影运算

投影是指将对象转换为一种新形式的操作,该形式通常只包含那些将随后使用的属性。通过使用投影,您可以构建依据每个对象生成的新类型。您可以映射属性,并对该属性执行数学函数。还可以在不更改原始对象的情况下映射该对象。

下面一节中列出了执行投影的标准查询运算符方法。

方法

方法名

说明

C# 查询表达式语法

Visual Basic 查询表达式语法

更多信息

Select

映射基于转换函数的值。

select

Select

Enumerable..::.Select

Queryable..::.Select

SelectMany

映射基于转换函数的值序列,然后将它们展平为一个序列。

使用多个 from 子句

使用多个 From 子句

Enumerable..::.SelectMany

Queryable..::.SelectMany

查询表达式语法示例

Select

下面的示例使用 C# 中的 select 子句或 Visual Basic 中的 Select 子句来映射字符串列表中每个字符串的第一个字母。

Visual Basic 复制代码Dim words As New List(Of String)(New String() {"an", "apple", "a", "day"})

Dim query = From word In words _

Select word.Substring(0, 1)

Dim sb As New System.Text.StringBuilder()

For Each letter As String In query

sb.AppendLine(letter)

Next

' Display the output.

MsgBox(sb.ToString())

' This code produces the following output:

' a

' a

' a

' d

C# 复制代码List<string> words = new List<string>() { "an", "apple", "a", "day" };

var query = from word in words

select word.Substring(0, 1);

foreach (string s in query)

Console.WriteLine(s);

/* This code produces the following output:

a

a

a

d

*/

SelectMany

下面的示例使用多个 from 子句(在 C# 中)或 From 子句(在 Visual Basic 中)来映射字符串列表中每个字符串中的每个单词。

Visual Basic 复制代码Dim phrases As New List(Of String)(New String() {"an apple a day", "the quick brown fox"})

Dim query = From phrase In phrases _

From word In phrase.Split(" "c) _

Select word

Dim sb As New System.Text.StringBuilder()

For Each str As String In query

sb.AppendLine(str)

Next

' Display the output.

MsgBox(sb.ToString())

' This code produces the following output:

' an

' apple

' a

' day

' the

' quick

' brown

' fox

C# 复制代码List<string> phrases = new List<string>() { "an apple a day", "the quick brown fox" };

var query = from phrase in phrases

from word in phrase.Split(' ')

select word;

foreach (string s in query)

Console.WriteLine(s);

/* This code produces the following output:

an

apple

a

day

the

quick

brown

fox

*/

Select 与 SelectMany

Select() 和 SelectMany() 的工作都是依据源值生成一个或多个结果值。Select() 为每个源值生成一个结果值。因此,总体结果是一个与源集合具有相同元素数目的集合。与之相反,SelectMany() 将生成单一总体结果,其中包含来自每个源值的串联子集合。作为参数传递到 SelectMany() 的转换函数必须为每个源值返回一个可枚举值序列。然后,SelectMany() 将串联这些可枚举序列以创建一个大的序列。

下面两个插图演示了这两个方法的操作之间的概念性区别。在每种情况下,假定选择器(转换)函数从每个源值中选择一个由花卉数据组成的数组。

下图描述 Select() 如何返回一个与源集合具有相同元素数目的集合。

下图描述 SelectMany() 如何将中间数组序列串联为一个最终结果值,其中包含每个中间数组中的每个值。

代码示例

下面的示例比较 Select() 和 SelectMany() 的行为。代码将通过从源集合的每个花卉名称列表中提取前两项来创建一个“花束”。在此示例中,转换函数 Select<(Of <(TSource, TResult>)>)(IEnumerable<(Of <(TSource>)>), Func<(Of <(TSource, TResult>)>)) 使用的“单一值”本身就是一个值集合。这需要额外的 foreach(Visual Basic 中为 For Each)循环,以便枚举每个子序列中的每个字符串。

Visual Basic 复制代码Class Bouquet

Public Flowers As List(Of String)

End Class

Sub SelectVsSelectMany()

Dim bouquets As New List(Of Bouquet)(New Bouquet() { _

New Bouquet With {.Flowers = New List(Of String)(New String() {"sunflower", "daisy", "daffodil", "larkspur"})}, _

New Bouquet With {.Flowers = New List(Of String)(New String() {"tulip", "rose", "orchid"})}, _

New Bouquet With {.Flowers = New List(Of String)(New String() {"gladiolis", "lily", "snapdragon", "aster", "protea"})}, _

New Bouquet With {.Flowers = New List(Of String)(New String() {"larkspur", "lilac", "iris", "dahlia"})}})

Dim output As New System.Text.StringBuilder

' Select()

Dim query1 = bouquets.Select(Function(b) b.Flowers)

output.AppendLine("Using Select():")

For Each flowerList In query1

For Each str As String In flowerList

output.AppendLine(str)

Next

Next

' SelectMany()

Dim query2 = bouquets.SelectMany(Function(b) b.Flowers)

output.AppendLine(vbCrLf & "Using SelectMany():")

For Each str As String In query2

output.AppendLine(str)

Next

' Display the output

MsgBox(output.ToString())

' This code produces the following output:

'

' Using Select():

' sunflower

' daisy

' daffodil

' larkspur

' tulip

' rose

' orchid

' gladiolis

' lily

' snapdragon

' aster

' protea

' larkspur

' lilac

' iris

' dahlia

' Using SelectMany()

' sunflower

' daisy

' daffodil

' larkspur

' tulip

' rose

' orchid

' gladiolis

' lily

' snapdragon

' aster

' protea

' larkspur

' lilac

' iris

' dahlia

End Sub

C# 复制代码class Bouquet

{

public List<string> Flowers { get; set; }

}

static void SelectVsSelectMany()

{

List<Bouquet> bouquets = new List<Bouquet>() {

new Bouquet { Flowers = new List<string> { "sunflower", "daisy", "daffodil", "larkspur" }},

new Bouquet{ Flowers = new List<string> { "tulip", "rose", "orchid" }},

new Bouquet{ Flowers = new List<string> { "gladiolis", "lily", "snapdragon", "aster", "protea" }},

new Bouquet{ Flowers = new List<string> { "larkspur", "lilac", "iris", "dahlia" }}

};

// *********** Select ***********

IEnumerable<List<string>> query1 = bouquets.Select(bq => bq.Flowers);

// ********* SelectMany *********

IEnumerable<string> query2 = bouquets.SelectMany(bq => bq.Flowers);

Console.WriteLine("Results by using Select():");

// Note the extra foreach loop here.

foreach (IEnumerable<String> collection in query1)

foreach (string item in collection)

Console.WriteLine(item);

Console.WriteLine("

Results by using SelectMany():");

foreach (string item in query2)

Console.WriteLine(item);

/* This code produces the following output:

Results by using Select():

sunflower

daisy

daffodil

larkspur

tulip

rose

orchid

gladiolis

lily

snapdragon

aster

protea

larkspur

lilac

iris

dahlia

Results by using SelectMany():

sunflower

daisy

daffodil

larkspur

tulip

rose

orchid

gladiolis

lily

snapdragon

aster

protea

larkspur

lilac

iris

dahlia

*/

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
如何用java替换看不见的字符比如零宽空格&#8203;十六进制U+200B
 干货   2023-09-10
网页字号不能单数吗,网页字体大小为什么一般都是偶数
 干货   2023-09-06
java.lang.ArrayIndexOutOfBoundsException: 4096
 干货   2023-09-06
Noto Sans CJK SC字体下载地址
 干货   2023-08-30
window.navigator和navigator的区别是什么?
 干货   2023-08-23
js获取referer、useragent、浏览器语言
 干货   2023-08-23
oscache遇到404时会不会缓存?
 干货   2023-08-23
linux下用rm -rf *删除大量文件太慢怎么解决?
 干货   2023-08-08
刀郎新歌破世界纪录!
 娱乐   2023-08-01
js实现放大缩小页面
 干货   2023-07-31
生成式人工智能服务管理暂行办法
 百态   2023-07-31
英语学习:过去完成时The Past Perfect Tense举例说明
 干货   2023-07-31
Mysql常用sql命令语句整理
 干货   2023-07-30
科学家复活了46000年前的虫子
 探索   2023-07-29
英语学习:过去进行时The Past Continuous Tense举例说明
 干货   2023-07-28
meta name="applicable-device"告知页面适合哪种终端设备:PC端、移动端还是自适应
 干货   2023-07-28
只用css如何实现打字机特效?
 百态   2023-07-15
css怎么实现上下滚动
 干货   2023-06-28
canvas怎么画一个三角形?
 干货   2023-06-28
canvas怎么画一个椭圆形?
 干货   2023-06-28
canvas怎么画一个圆形?
 干货   2023-06-28
canvas怎么画一个正方形?
 干货   2023-06-28
中国河南省郑州市金水区蜘蛛爬虫ip大全
 干货   2023-06-22
javascript简易动态时间代码
 干货   2023-06-20
感谢员工的付出和激励的话怎么说?
 干货   2023-06-18
 
>>返回首页<<
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有