Are You Using the Extended LINQ Operators?

  Рет қаралды 7,709

Zoran Horvat

Zoran Horvat

Күн бұрын

Пікірлер: 65
@aborum75
@aborum75 10 күн бұрын
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.
@nickbarton3191
@nickbarton3191 10 күн бұрын
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.
@aborum75
@aborum75 10 күн бұрын
@@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.
@nickbarton3191
@nickbarton3191 10 күн бұрын
@@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.
@PlerbyMcFlerb
@PlerbyMcFlerb 2 күн бұрын
@@nickbarton3191 totally. Life's too short to learn things or to self improve. Why bother?
@nickbarton3191
@nickbarton3191 2 күн бұрын
@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.
@petewarner1077
@petewarner1077 10 күн бұрын
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-horvat
@zoran-horvat 10 күн бұрын
@@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.
@krccmsitp2884
@krccmsitp2884 4 күн бұрын
public static void ForEach(this IEnumerable collection, Action callback) { foreach (T item in collection) { callback(item); } }
@elraito
@elraito 10 күн бұрын
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-horvat
@zoran-horvat 10 күн бұрын
@@elraito I didn't know that.
@Tesfamichael.G
@Tesfamichael.G 10 күн бұрын
Thank you Zoran!
@luc9volts
@luc9volts 10 күн бұрын
I didn't know that. Awesome. Thanks
@nickbarton3191
@nickbarton3191 10 күн бұрын
Nice video. Used all but zip, and the extra lamba for group by. Learn something new daily.
@kenbrady119
@kenbrady119 9 күн бұрын
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-horvat
@zoran-horvat 9 күн бұрын
@@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.
@okcharles7
@okcharles7 10 күн бұрын
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.
@aanders0n
@aanders0n 10 күн бұрын
When will your new course be released?
@zoran-horvat
@zoran-horvat 10 күн бұрын
@@aanders0n Soon, hopefully.
@tomprogrammer
@tomprogrammer 10 күн бұрын
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-horvat
@zoran-horvat 10 күн бұрын
@@tomprogrammer In the case of Zip, the overload with an additional lambda is the default in my designs. It is so natural.
@barionlp
@barionlp 10 күн бұрын
why use GroupBy and not CountBy?
@zoran-horvat
@zoran-horvat 10 күн бұрын
@@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.
@barionlp
@barionlp 10 күн бұрын
fair, maybe you should have mentioned CountBy so people don’t start using GroupBy when CountBy would be sufficient
@zoran-horvat
@zoran-horvat 10 күн бұрын
@barionlp True.
@oleksii766
@oleksii766 5 күн бұрын
.NET 9 comes with CountBy method which already groupes by key
@zoran-horvat
@zoran-horvat 5 күн бұрын
@@oleksii766 I am aware of that, but my intention was to show the GroupBy overload.
@nanny07
@nanny07 10 күн бұрын
Will this improve performances? Because if not, I prefer intuitiveness and readability
@zoran-horvat
@zoran-horvat 10 күн бұрын
@@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.
@aborum75
@aborum75 10 күн бұрын
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-horvat
@zoran-horvat 10 күн бұрын
@aborum75 It is the matter of judgment. I generally prefer readability unless performance is a concern.
@nanny07
@nanny07 10 күн бұрын
@@zoran-horvat thanks you for the response
@aborum75
@aborum75 10 күн бұрын
@@zoran-horvat agree, and I would argue that the overload operators are more readable.
@7th_CAV_Trooper
@7th_CAV_Trooper 9 күн бұрын
Have you played with Semantic Kernal yet?
@PlerbyMcFlerb
@PlerbyMcFlerb 10 күн бұрын
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-horvat
@zoran-horvat 10 күн бұрын
@@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.
@PlerbyMcFlerb
@PlerbyMcFlerb 9 күн бұрын
@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
@krccmsitp2884
@krccmsitp2884 4 күн бұрын
Here you go: public static void ForEach(this IEnumerable collection, Action callback) { foreach (T item in collection) { callback(item); } }
@zoran-horvat
@zoran-horvat 4 күн бұрын
@krccmsitp2884 I have that one in all my projects.
@brotherzero
@brotherzero 10 күн бұрын
"Zip" is such a confusing name. I know it refers to a zipper but most people hear "Zip" and think of file compression
@DavidSmith-ef4eh
@DavidSmith-ef4eh 10 күн бұрын
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
@nickbarton3191 10 күн бұрын
Let Resharper convert your procedural loops to Linq, then optimise if you can.
@DavidSmith-ef4eh
@DavidSmith-ef4eh 9 күн бұрын
@@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
@nickbarton3191
@nickbarton3191 9 күн бұрын
@@DavidSmith-ef4eh Defo, keep learning each whatever way you can.
@7th_CAV_Trooper
@7th_CAV_Trooper 9 күн бұрын
@@DavidSmith-ef4eh I suspect humans can learn new things. Even your coworkers. 😉
@nickbarton3191
@nickbarton3191 9 күн бұрын
@@7th_CAV_Trooper We have a responsibility to co-workers to guide and instruct and vice versa. We should be working co-operatively.
Make Domain Rules Explicit In Any Business Application
9:23
Zoran Horvat
Рет қаралды 16 М.
C# Minimal APIs Quickly Get Messy - Clean Them Up!
12:41
Zoran Horvat
Рет қаралды 10 М.
СИНИЙ ИНЕЙ УЖЕ ВЫШЕЛ!❄️
01:01
DO$HIK
Рет қаралды 3,3 МЛН
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
Mom Hack for Cooking Solo with a Little One! 🍳👶
00:15
5-Minute Crafts HOUSE
Рет қаралды 23 МЛН
10 Essential Constructors in C# Every Developer Should Know
12:52
Zoran Horvat
Рет қаралды 12 М.
Why is every React site so slow?
13:52
Theo - t3․gg
Рет қаралды 151 М.
Fixing Your Dictionary Problem in .NET
9:19
Nick Chapsas
Рет қаралды 30 М.
10 Signs Your Software Project Is Heading For FAILURE
17:59
Continuous Delivery
Рет қаралды 40 М.
The Combinatorial Explosion Trap: Does Your Code Have It?
13:54
Zoran Horvat
Рет қаралды 10 М.
5 deadly Rust anti-patterns to avoid
13:25
Let's Get Rusty
Рет қаралды 40 М.
Pilot: A Game Show for Web Developers - Leet Heat S1E1
23:43
Learn With Jason
Рет қаралды 33 М.
Eliminate Data Clumps: Step-by-Step Refactoring Guide
14:24
Zoran Horvat
Рет қаралды 12 М.
The Only Cache You Should Be Using in .NET
12:22
Nick Chapsas
Рет қаралды 32 М.
Why Applications Are Operating-System Specific
13:09
Core Dumped
Рет қаралды 159 М.