为什么 LINQ 是“适用于所有”的?名为 Select? 的方法

发布于 2024-08-18 12:54:11 字数 255 浏览 4 评论 0原文

当我阅读使用 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.

Map

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

明天过后 2024-08-25 12:54:12

起初,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 for Select. Other than that I feel Select conveys the immutability aspect of Linq much better. Not that Map would mean it's mutating the original structure, but Select 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 call collection.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 the Linq namings, now I feel MS team has got it the most correct.

欲拥i 2024-08-25 12:54:12

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.

尛丟丟 2024-08-25 12:54:11

它与函数式语言的映射非常相似。它被命名为 Select 的原因是它被设计为用作使用类似 SQL 关键字的 LINQ 的一部分。

from item in collection
where item.Value == someValue
select item.Name

翻译为:

collection.Where(item => item.Value == someValue)
          .Select(item => item.Name)

如果Select被命名为Map,会有点不一致;例如:

collection.Filter(item => item.Value == someValue)
          .Map(item => item.Name)

事实上,许多人使用 LINQ 时根本没有听说过函数式编程。对他们来说,LINQ 是一种检索数据对象并轻松查询它们的方法(就像 SQL 查询一样)。对他们来说,SelectWhere 非常有意义。不仅仅是MapFilter

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

is translated to:

collection.Where(item => item.Value == someValue)
          .Select(item => item.Name)

it would be a little inconsistent if Select was named Map; something like:

collection.Filter(item => item.Value == someValue)
          .Map(item => item.Name)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文