Highly recommend going through an exercise of using all of the LINQ operators, including their respective overloads. It's knowledge that helps you write as efficient and elegant queries as shown in Zoran's video.
I watch a TH-cam video that went through all of them in 15 minutes, all 60+ of them. Life's too short to write exercises to understand them all, engineers don't read documentation unless all fails, and anyway now we have AI, just describe the problem and tell it to use LInq, then read the documentation. Edit. I should've prefaced this with I didn't watch the video yet. Indeed, Zoran is awesome, learnt so much from him.
@@nickbarton3191 there's so much going in your comment that it's hard to provide a sensible reply. Unless you learn better patterns, everything remains a hammer.
@@aborum75 Sufficient to say that I'm using tools to create Linq statements. Either ReSharper to convert loops, CoPilot from a comment, sometimes I write it myself. What I'm really trying to say there's no reason not to use Linq. Occasionally, I found that a procedural solution is more flexible. One time, Zoran's example used tuples, then you're stuck with the constraints of the tuple definition; didn't like that. Just about to watch the video.
It should be noted that the ToList().ForEach(...) style comes at the expense of a list allocation that's not present in the foreach(var thing in things) style. A nice compromise is to write a ForEach extension on IEnumerable that wraps a foreach - but bear in mind that should be regarded as a terminating operator, ie it causes side effects and should not be considered a functionally pure LINQ extension. So use it wisely, and even then, perhaps name it Iterate() instead of ForEach()
@@petewarner1077 Yes, but it also comes with Console.WriteLine, which admits there are only a few items. On a larger sequence I would use a custom ForEach defined on IEnumerable.
Awesome video as always. Seems Zorans videos are always of type Option.Great. as for topic, the overloads are very common in legacy code with nhibernate
Thank you for these reminders of how we can better use LINQ. However, I'm concerned by your use of the ToList method which transforms the enumeration into a List. My understanding of List is that it creates an array[T] to store the values. Is this memory allocation necessary? Is it the only way to simply "ForEach" the elements? Why doesn't LINQ itself have a ForEach(Action) overload?
@@kenbrady119 LINQ doesn't have ForEach because it is a functional library and ForEach produces side effects by default. However, I often define my own ForEach on IEnumerable and then use it with discipline. Regarding the use of ToList in the demo, you shouldn't worry. The greatest threat comes from the call to WriteLine in the loop, which would take forever if the list were large. Therefore, collecting into a list is a lesser problem there.
I know you were trying to be as mild as possible for foreachers. For whoever wants chilly flavor, I strongly recommend his tutorial on the object oriented programming in Udemy or other places.
I'm not fully convinced yet, that combining a `Select` into another LINQ operator via the overload is strictly more readable. `Zip` should zip two `IEnumerable`s, one task for one method. But in the overload it zips and selects in only one method call. This seems to conflate operations. But on the other hand this overload is typical for LINQ operators it seems, which is in contrast to my previous reasoning. So it may actually be very readable, if the overload is well known.
@@nanny07 It surely does improve performance, as there will be one lazy-evaluated sequence less to construct and iterate, as well as all the intermediate objects that will not be created.
As these matters are highly subjective, there's no denying that fever chained operators leads to better performance. I would argue that readbility is the most important part (usually), and as is evident from Zoran's video, using the overloads may help in this matter.
@@barionlp That is a good question and the answer might not satisfy you - I wanted to showcase the GroupBy. I was thinking about isolating the range of dates or similar complex expressions, but that might be too much for many viewers.
Don't get me wrong, I'm a huge LINQ stan... but isn't ToList().ForEach() a bit silly? Why materialize everything into a list just for method chaining? I'd say either bite the bullet and foreach, or write your own ForEach extension method that evaluates the linq expression and performs an action per item.
@@PlerbyMcFlerb I know what you are referring to. I have an IEnumerable extension ForEach for side effect-producing actions - you will find it even in the project from the video, in the Common namespace. There are two reasons why I relied on the list in the demo. First, using my custom extension might make some viewers think there exists such a method, where it doesn't. The second reason is more interesting, as it is the result of applying indirect logic. I'm sending the strings to the console. The list must be small then, or otherwise I wouldn't be printing it out. Using ToList was an Easter egg after all, that not many will discover after all.
@zoran-horvat yeah that all makes sense. I've just seen people do it in production where it was a poor decision, so thought it was worth a mention. Love your content, it's got a really unique delivery to it
Don't know if I am a fan of it. This is like very advanced SQL. It doesn't sound intuitive and very few people can understand and do it. I rather have a foreach loop with some procedural code inside of it. Of course, like in SQL i do like the common ones in LINQ as well. Everyone should be able to know the 5-6 most common ones.
@@nickbarton3191 ai could do it as well. but either way.. even if you can do it, it doesn't mean your coworkers can. Tbf, I do have low-level coworkers. Not that I am much better, but at least I keep learning :D
Highly recommend going through an exercise of using all of the LINQ operators, including their respective overloads. It's knowledge that helps you write as efficient and elegant queries as shown in Zoran's video.
I watch a TH-cam video that went through all of them in 15 minutes, all 60+ of them. Life's too short to write exercises to understand them all, engineers don't read documentation unless all fails, and anyway now we have AI, just describe the problem and tell it to use LInq, then read the documentation.
Edit. I should've prefaced this with I didn't watch the video yet. Indeed, Zoran is awesome, learnt so much from him.
@@nickbarton3191 there's so much going in your comment that it's hard to provide a sensible reply.
Unless you learn better patterns, everything remains a hammer.
@@aborum75 Sufficient to say that I'm using tools to create Linq statements. Either ReSharper to convert loops, CoPilot from a comment, sometimes I write it myself. What I'm really trying to say there's no reason not to use Linq. Occasionally, I found that a procedural solution is more flexible. One time, Zoran's example used tuples, then you're stuck with the constraints of the tuple definition; didn't like that.
Just about to watch the video.
It should be noted that the ToList().ForEach(...) style comes at the expense of a list allocation that's not present in the foreach(var thing in things) style.
A nice compromise is to write a ForEach extension on IEnumerable that wraps a foreach - but bear in mind that should be regarded as a terminating operator, ie it causes side effects and should not be considered a functionally pure LINQ extension. So use it wisely, and even then, perhaps name it Iterate() instead of ForEach()
@@petewarner1077 Yes, but it also comes with Console.WriteLine, which admits there are only a few items. On a larger sequence I would use a custom ForEach defined on IEnumerable.
Awesome video as always. Seems Zorans videos are always of type Option.Great. as for topic, the overloads are very common in legacy code with nhibernate
@@elraito I didn't know that.
Thank you Zoran!
I didn't know that. Awesome. Thanks
Nice video.
Used all but zip, and the extra lamba for group by. Learn something new daily.
Thank you for these reminders of how we can better use LINQ. However, I'm concerned by your use of the ToList method which transforms the enumeration into a List. My understanding of List is that it creates an array[T] to store the values. Is this memory allocation necessary? Is it the only way to simply "ForEach" the elements? Why doesn't LINQ itself have a ForEach(Action) overload?
@@kenbrady119 LINQ doesn't have ForEach because it is a functional library and ForEach produces side effects by default. However, I often define my own ForEach on IEnumerable and then use it with discipline.
Regarding the use of ToList in the demo, you shouldn't worry. The greatest threat comes from the call to WriteLine in the loop, which would take forever if the list were large. Therefore, collecting into a list is a lesser problem there.
I know you were trying to be as mild as possible for foreachers.
For whoever wants chilly flavor, I strongly recommend his tutorial on the object oriented programming in Udemy or other places.
Have you played with Semantic Kernal yet?
I'm not fully convinced yet, that combining a `Select` into another LINQ operator via the overload is strictly more readable. `Zip` should zip two `IEnumerable`s, one task for one method. But in the overload it zips and selects in only one method call. This seems to conflate operations. But on the other hand this overload is typical for LINQ operators it seems, which is in contrast to my previous reasoning. So it may actually be very readable, if the overload is well known.
@@tomprogrammer In the case of Zip, the overload with an additional lambda is the default in my designs. It is so natural.
Will this improve performances? Because if not, I prefer intuitiveness and readability
@@nanny07 It surely does improve performance, as there will be one lazy-evaluated sequence less to construct and iterate, as well as all the intermediate objects that will not be created.
As these matters are highly subjective, there's no denying that fever chained operators leads to better performance. I would argue that readbility is the most important part (usually), and as is evident from Zoran's video, using the overloads may help in this matter.
@aborum75 It is the matter of judgment. I generally prefer readability unless performance is a concern.
@@zoran-horvat thanks you for the response
@@zoran-horvat agree, and I would argue that the overload operators are more readable.
When will your new course be released?
@@aanders0n Soon, hopefully.
why use GroupBy and not CountBy?
@@barionlp That is a good question and the answer might not satisfy you - I wanted to showcase the GroupBy. I was thinking about isolating the range of dates or similar complex expressions, but that might be too much for many viewers.
fair, maybe you should have mentioned CountBy so people don’t start using GroupBy when CountBy would be sufficient
@barionlp True.
Don't get me wrong, I'm a huge LINQ stan... but isn't ToList().ForEach() a bit silly? Why materialize everything into a list just for method chaining?
I'd say either bite the bullet and foreach, or write your own ForEach extension method that evaluates the linq expression and performs an action per item.
@@PlerbyMcFlerb I know what you are referring to. I have an IEnumerable extension ForEach for side effect-producing actions - you will find it even in the project from the video, in the Common namespace.
There are two reasons why I relied on the list in the demo. First, using my custom extension might make some viewers think there exists such a method, where it doesn't. The second reason is more interesting, as it is the result of applying indirect logic. I'm sending the strings to the console. The list must be small then, or otherwise I wouldn't be printing it out. Using ToList was an Easter egg after all, that not many will discover after all.
@zoran-horvat yeah that all makes sense. I've just seen people do it in production where it was a poor decision, so thought it was worth a mention. Love your content, it's got a really unique delivery to it
"Zip" is such a confusing name. I know it refers to a zipper but most people hear "Zip" and think of file compression
Don't know if I am a fan of it. This is like very advanced SQL. It doesn't sound intuitive and very few people can understand and do it. I rather have a foreach loop with some procedural code inside of it. Of course, like in SQL i do like the common ones in LINQ as well. Everyone should be able to know the 5-6 most common ones.
Let Resharper convert your procedural loops to Linq, then optimise if you can.
@@nickbarton3191 ai could do it as well. but either way.. even if you can do it, it doesn't mean your coworkers can. Tbf, I do have low-level coworkers. Not that I am much better, but at least I keep learning :D
@@DavidSmith-ef4eh Defo, keep learning each whatever way you can.
@@DavidSmith-ef4eh I suspect humans can learn new things. Even your coworkers. 😉
@@7th_CAV_Trooper We have a responsibility to co-workers to guide and instruct and vice versa. We should be working co-operatively.