为什么 LINQ 是“适用于所有”的?名为 Select? 的方法
当我阅读使用 Select
的代码时,我想到“select-all-where”。 当我阅读使用 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
起初,
Select
对我来说似乎也没什么困惑,但这只是时间问题。 Mehrdad 告诉您Select
的一个很好的理由。除此之外,我觉得Select
更好地传达了Linq
的不变性方面。并不是说Map
意味着它会改变原始结构,而是Select
表述得更清楚。它告诉您没有触及原始列表,而只是从中进行选择以形成另一个列表。它也可以与其他命名一起使用,例如
Where
。当您调用collection.Filter
时,它会让您知道您正在过滤该特定集合,或者至少是第一次。归根结底,这只是熟悉的问题。虽然一开始我对Linq
命名很恼火,但现在我觉得 MS 团队的命名是最正确的。At first
Select
seemed little confusing for me too, but it was only a matter of time. Mehrdad tells you a good reason forSelect
. Other than that I feelSelect
conveys the immutability aspect ofLinq
much better. Not thatMap
would mean it's mutating the original structure, butSelect
states it much clearer. It tells you're not touching the original list but merely selecting from it to form another list.It goes with other naming as well like
Where
. When you callcollection.Filter
it gives you an idea that you're filtering on that particular collection, or at least the first time. In the end it's all a matter of getting familiarized. Though in the beginning I was so annoyed by theLinq
namings, now I feel MS team has got it the most correct.Select 排在最后的主要原因之一是让 Intellisense 发挥作用。通过将序列的来源放在第一位(from 语句),Intellisense 可以正常工作。
One of the major reasons Select comes last is to make Intellisense work. By putting the source of the sequence first (from statement), Intellisense can work properly.
它与函数式语言的映射非常相似。它被命名为
Select
的原因是它被设计为用作使用类似 SQL 关键字的 LINQ 的一部分。翻译为:
如果
Select
被命名为Map
,会有点不一致;例如:事实上,许多人使用 LINQ 时根本没有听说过函数式编程。对他们来说,LINQ 是一种检索数据对象并轻松查询它们的方法(就像 SQL 查询一样)。对他们来说,
Select
和Where
非常有意义。不仅仅是Map
和Filter
。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.is translated to:
it would be a little inconsistent if
Select
was namedMap
; something like: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
andWhere
make perfect sense. Much more thanMap
andFilter
.