←back to thread

201 points olvy0 | 5 comments | | HN request time: 0.001s | source
Show context
olvy0 ◴[] No.41879131[source]
For me, the most useful part of LINQ is neither the IQueryable syntax tree based extension mechanism, nor the language integrated part (which I dislike), but simply the IEnumerable extensions. Originally known somewhat confusingly as linq to objects. Those allow me to write c# in functional style, keeping the code concise.

The post I submitted refers mostly to optimizations to those extension methods.

This clicked for me after having learned Haskell. It also shares some of Haskell's features/pitfalls, such as laziness.

There are pitfalls, sure, and honestly I wouldn't advise a team having no one somewhat experienced with basic functional idioms (including laziness) to use it. It can lead to obtuse and slow code if used indiscriminately. I try to lead by example myself.

replies(6): >>41879608 #>>41879713 #>>41882332 #>>41882614 #>>41883044 #>>41883519 #
sedatk ◴[] No.41882614[source]
Totally. I couldn't care less if the LINQ syntax had gone tomorrow, but functional composition is so powerful, and easier to maintain too.
replies(2): >>41883052 #>>41884395 #
recursive ◴[] No.41883052[source]
I would miss it for `let`, and for multiple `from` clauses. These are things that can be done with extension methods and callbacks, just not as elegantly.
replies(1): >>41883168 #
sedatk ◴[] No.41883168{3}[source]
`let` is just a .Select(), isn't it?
replies(1): >>41883467 #
1. brewmarche ◴[] No.41883467{4}[source]
A `Select` to a tuple or anonymous class which you have to carry around all the way.

If you look at how a LINQ query gets translated to a method chain (e.g. via SharpLab or let Rider/ReSharper rewrite them) you'll notice that multiple `from`s or `let`s can become quite ugly.

replies(2): >>41883555 #>>41883791 #
2. progmetaldev ◴[] No.41883555[source]
To add on to what you wrote, it amazes me the number of developers that don't understand you can use .Select() to construct named object instances. I'm probably using bad terminology, but something like:

    .Select(x => new Employee { Name = x.Name, DepartmentName = x.Department.Name }).ToList();
replies(1): >>41883661 #
3. recursive ◴[] No.41883661[source]
How do these developers use .Select() then?
replies(1): >>41884788 #
4. sedatk ◴[] No.41883791[source]
Oh that's convenient, that's for sure.
5. progmetaldev ◴[] No.41884788{3}[source]
Into anonymous types mostly, where you can't pass the type to another method. Example:

    .Select(x => new { Name = x.Name, DepartmentName = x.Department.Name }).ToList();
Same code as I wrote, except capture in an anonymous type. The documentation practically encourages this.