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.
@nickbarton319110 күн бұрын
I watch a KZbin 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.
@aborum7510 күн бұрын
@@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.
@nickbarton319110 күн бұрын
@@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.
@PlerbyMcFlerb2 күн бұрын
@@nickbarton3191 totally. Life's too short to learn things or to self improve. Why bother?
@nickbarton31912 күн бұрын
@PlerbyMcFlerb thats not what I meant, you twist my words. I learn when it's necessary, on the job. To go through exercises for just Linq when there is so much more to learn? Well just yesterday, I solved a complex math problem, for work, exclusively with Linq. Learning is better when it's applied, solve the problem for the client, or starve.
@petewarner107710 күн бұрын
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()
@zoran-horvat10 күн бұрын
@@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.
@krccmsitp28844 күн бұрын
public static void ForEach(this IEnumerable collection, Action callback) { foreach (T item in collection) { callback(item); } }
@elraito10 күн бұрын
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
@zoran-horvat10 күн бұрын
@@elraito I didn't know that.
@Tesfamichael.G10 күн бұрын
Thank you Zoran!
@luc9volts10 күн бұрын
I didn't know that. Awesome. Thanks
@nickbarton319110 күн бұрын
Nice video. Used all but zip, and the extra lamba for group by. Learn something new daily.
@kenbrady1199 күн бұрын
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?
@zoran-horvat9 күн бұрын
@@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.
@okcharles710 күн бұрын
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.
@aanders0n10 күн бұрын
When will your new course be released?
@zoran-horvat10 күн бұрын
@@aanders0n Soon, hopefully.
@tomprogrammer10 күн бұрын
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.
@zoran-horvat10 күн бұрын
@@tomprogrammer In the case of Zip, the overload with an additional lambda is the default in my designs. It is so natural.
@barionlp10 күн бұрын
why use GroupBy and not CountBy?
@zoran-horvat10 күн бұрын
@@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.
@barionlp10 күн бұрын
fair, maybe you should have mentioned CountBy so people don’t start using GroupBy when CountBy would be sufficient
@zoran-horvat10 күн бұрын
@barionlp True.
@oleksii7665 күн бұрын
.NET 9 comes with CountBy method which already groupes by key
@zoran-horvat5 күн бұрын
@@oleksii766 I am aware of that, but my intention was to show the GroupBy overload.
@nanny0710 күн бұрын
Will this improve performances? Because if not, I prefer intuitiveness and readability
@zoran-horvat10 күн бұрын
@@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.
@aborum7510 күн бұрын
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.
@zoran-horvat10 күн бұрын
@aborum75 It is the matter of judgment. I generally prefer readability unless performance is a concern.
@nanny0710 күн бұрын
@@zoran-horvat thanks you for the response
@aborum7510 күн бұрын
@@zoran-horvat agree, and I would argue that the overload operators are more readable.
@7th_CAV_Trooper9 күн бұрын
Have you played with Semantic Kernal yet?
@PlerbyMcFlerb10 күн бұрын
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.
@zoran-horvat10 күн бұрын
@@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.
@PlerbyMcFlerb9 күн бұрын
@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
@krccmsitp28844 күн бұрын
Here you go: public static void ForEach(this IEnumerable collection, Action callback) { foreach (T item in collection) { callback(item); } }
@zoran-horvat4 күн бұрын
@krccmsitp2884 I have that one in all my projects.
@brotherzero10 күн бұрын
"Zip" is such a confusing name. I know it refers to a zipper but most people hear "Zip" and think of file compression
@DavidSmith-ef4eh10 күн бұрын
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.
@nickbarton319110 күн бұрын
Let Resharper convert your procedural loops to Linq, then optimise if you can.
@DavidSmith-ef4eh9 күн бұрын
@@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
@nickbarton31919 күн бұрын
@@DavidSmith-ef4eh Defo, keep learning each whatever way you can.
@7th_CAV_Trooper9 күн бұрын
@@DavidSmith-ef4eh I suspect humans can learn new things. Even your coworkers. 😉
@nickbarton31919 күн бұрын
@@7th_CAV_Trooper We have a responsibility to co-workers to guide and instruct and vice versa. We should be working co-operatively.