为什么LINQ“全部应用"?名为Select的方法? [英] Why is the LINQ "apply-to-all" method named Select?
问题描述
当我阅读使用Select
的代码时,我认为是全选".
当我阅读使用Map
的代码时,我会认为此到那个"或全部应用".
我不能成为唯一一个感到Select
这个名字令人困惑的人.
When I read code that uses Select
I think "select-all-where".
When I read code that uses Map
I think "this-to-that" or "apply-to-all".
I can't be the only person that feels the name Select
is confusing.
推荐答案
它与功能语言中的映射完全相同.之所以命名为Select
,是因为它被设计为LINQ的一部分,而LINQ使用的是类似SQL的关键字.
It's really identical to map from functional languages. The reason it's named Select
is that it's designed to be used as a part of LINQ which uses SQL-like keywords.
from item in collection
where item.Value == someValue
select item.Name
被翻译为:
collection.Where(item => item.Value == someValue)
.Select(item => item.Name)
如果Select
被命名为Map
,将会有点不一致;像这样:
it would be a little inconsistent if Select
was named Map
; something like:
collection.Filter(item => item.Value == someValue)
.Map(item => item.Name)
实际上,许多人完全不使用函数式编程就使用LINQ.对于他们来说,LINQ是一种检索数据对象并轻松查询它们的方法(就像SQL查询一样).对于他们来说,Select
和Where
是很有意义的.比Map
和Filter
多得多.
In fact, many people use LINQ without having heard of functional programming at all. To them, LINQ is a method to retrieve data objects and query them easily (like SQL queries are). To them, Select
and Where
make perfect sense. Much more than Map
and Filter
.
这篇关于为什么LINQ“全部应用"?名为Select的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!