Short-hand syntax for IEnumerable.Select #845
Replies: 12 comments
-
I'm not sold on this syntax yet, but I do feel like this idea would improve a common scenario. |
Beta Was this translation helpful? Give feedback.
-
people..[Friends.[Name]] I find code like this unreadable. Instead, I would use query syntax, which I find much easier to understand: from person in people
from friend in person.Friends
select friend.Name Most code is written once and then read many times, so readability trumps saving keystrokes. Also, one of the strengths of LINQ is that you can easily combine various operations, it's not just |
Beta Was this translation helpful? Give feedback.
-
@svick I disagree. I find it more readable (though the exact syntax might need some work). Array indexing is similar. I don't think people have generally replaced |
Beta Was this translation helpful? Give feedback.
-
The simplest cases look alright to me, but I find |
Beta Was this translation helpful? Give feedback.
-
Not sure what you mean in the context of arrays. |
Beta Was this translation helpful? Give feedback.
-
I follow your logic, but if something like the query continuation were not possible, then it would surely be useless, for example. var x = people.[Name].Where(s => s.Length > 3); Of course, my syntax is only an example, I am sure people more used to making compilers could think of a better syntax that would be easier to read. However, I'd rather have a one-liner than write a three query sentences (in query syntax), if symbols were known to me. The same happened with null propagation operator. It's confused and not readable if you encounter it the first time, but once you know it, then the Also I guess with something like Roslyn/#5445, it would become unneccessary. |
Beta Was this translation helpful? Give feedback.
-
That method could call the indexer of a known interface, sure, but it can't call an arbitrary indexer. And that behavior is completely hidden from the compiler which has no concept of |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I see your point. I really only intended to make the comparison based on readability (not implementation). I'm not sure I have a better idea than the OP for syntax. Here are a couple that come to mind: people[#Name] people[.Name] |
Beta Was this translation helpful? Give feedback.
-
People already complain that LINQ chains are hard to read. This would be that 100X. Plus, we don't need a 3rd syntax for doing LINQ. |
Beta Was this translation helpful? Give feedback.
-
How about shorter var x = people.[Name].[Length > 3]; This would take care of the exemplary cases at #91 and #602. |
Beta Was this translation helpful? Give feedback.
-
@bondsbw I think that takes a bad idea and makes it much worse:
|
Beta Was this translation helpful? Give feedback.
-
Good points. This will require at least a subtly different syntax, I'm open to ideas. (
I think I might prefer the syntax |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Starting with an example class:
Having an
IEnumerable<Person>
, we can select, for example, onlyNames
, like this:Also, with
SelectMany
, we can go further, like this:Since
Select
is one of the most used operators, I was thinking about having a syntactic sugar that would work onIEnumerable<T>
(maybeIQueryable<T>
if possible) to shorten this kind of behaviour to:or some other syntax (this is only an example).
Also,
SelectMany
version could be useful too, but I am not sure about the implications.Maybe all of this is not possible and maybe useful only if a delegate used in
Select
is only a property access, but still it might simplify code a bit for LINQ queries.I would like to hear both the pros and cons. Even if people find this useless, I'd like to hear it. 😄
Beta Was this translation helpful? Give feedback.
All reactions