7 awesome improvements for LINQ in .NET 6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody i'm nick and this we are going to take a look at seven new or enhanced link apis in dot net six now link is a hugely used feature in dot net in general and it's nice to see that it's still being built upon and modernized most of the changes we're gonna see especially the enhancements are effectively links apis trying to catch up with c sharp eight nine and ten and i'm gonna explain why i say that as we're going through the features especially when we're gonna attach an indexing and ranges but also nullability and then the new ones are more like quality of life improvements or things that you'd have to write on your own because to be honest they would very common use cases and it was weird that they were not in link to begin with we have quite a few of them to cover so without any further ado let's go straight into the code if you like a type of content and you want to see more make sure you subscribe bring the certification bell to get highlighted when i upload a new video so for the first feature before i tell you what it is let me tell you the problem right so i have a few names here an innumerable of names here it's an array and what i want to do is i want to split this array into arrays of equal sizes what do i mean well i wanna have an array or an enumerable in general with nick mike and john and then another one with uh layla david and damian right something like that now in the old world before this new feature i actually had to write my own method to do what is called chunking it would probably look something like this so we would have a chunk buy method where we accept an ironable of some type and then we have the chunk size for example chunks of three and then we'd have some link to do the chunking and if i was to use that it would look something like this let me just say chunked equals chunk by names coma three so chunk by three chunk size and let me just quickly add a console.read key here because this will exit if i don't have something android just because of the top level statements so i step over that and now with that method we have the two chunks right nick mike john layla david damian very very nice however this code is not the greatest and to be honest many people had to write their own approach to this because look this is a very very useful thing to have let's say you have a huge enumerable of like 10 000 things and then you want to split into batches because you want to do some batch processing and to make it more efficient you want to make use of your course on your cpu and you want to do some parallel processing and split them into equal pieces you'd use something like that well now in dot net six what you can do instead is you can say let me just comment that out you're going to say well chunk equals names.chunk 3. same api you have the enumerable that's an extension method and you're going to split into however many pieces with three items each and if i step over that as you can see we have now the two arrays and that's way more efficient that's what we have uh below as well so i recommend you use that if you have that use case i personally do that's why this is first very nice handy api now the next one is quite a bit of hard one to understand if you don't exactly know how innumerable works i'm gonna try to explain the problem first and then show you the solution if you can't really wrap your head around this that's fine i will make a video on annual in the future and if i have made that it will be on the top right on your screen right now but let me explain what the problem is let's say i have something like this i have three arrays and then i'm making an innumerable out of them with a concat method now at this point this immobile is just instructions on how to build that enumerable in memory when this line is stepped over we don't have those names we have them when i actually want to do something with them and this could be let's say get account so let's say names.count as you can see count is a method it's not a property and that is because it will actually enumerate the enumerable to give me the count now the problem is that if i want to do something like ordered names here and i say names.order by um the name itself you can see that i get those yellow squiggly lines the reason why i do that and you can see actually the the warning i'm getting is possible multiple enumeration what does that mean well that means that because immobile is effectively just instructions on how to build that enumerable memory it will be executed twice it will enumerate that thing twice and this could be like an io operation database call file system call networking call to collect that data back to me and again if i debug this just to understand exactly why i'm saying that you know when i'm stepping over this i do not have the names here now the debugger will give me the names and i'm going to show you when this is saying expand will force enumeration of the object this means that the moment i hit this arrow character thing it will actually go and execute this piece of code and concat them and then i have them in an array now when i click the thing to expand that's when this piece of code was executed i did not have it before now the problem is that if i do count and if i do order by this will enumerate twice which means more computation more memory it might be more network calls depending on what your innumerable is which we don't want and the reason why i'm using count is because count has been a very common thing to do before you want to do some other action whether that's pagination or batching or anything people will try to get the count of an ironable and enumerably trying to be smart about how count works um it will try to detect whether it's an eye collection because that has account or a list provider which has a efficiency method type thing for the account then i collection has a count or else it will loop through the enumerator and give you the account back meaning it enumerates everything but what has been added now in dot net six is the names dot try get non-enumerable count which other that being a very hard thing for me to pronounce is a very handy method because what this says is effectively trying to give me the count without forcing an enumeration if you have to enumerate then fail this if check give me false if you succeed then give me the count and return true here and then i can use the count without forcing an enumeration meaning well i'm not enumerating twice so you know this this warning this possible enumeration thing will not be valid if the ironwork behind the scenes is something that doesn't require enumeration in our example and let me just execute that here we go see this thing did not require an enumeration it did not require to force the enumeration for me to get the thing back and actually the dotnet team has a great documentation of this thing you can see here in the method attempts to determine the number of elements in a sequence without forcing enumeration this forcing enumeration thing is the expensive bit the hard thing that we don't want to have and we'll do that by using internal link apis and also this type checking if checks so very handy because then you can get the count and do something with that without having forced an enumeration very very useful thing now the next one is zipping imagine we have two different enumerables one that has names and one that has edges what i can do is i can zip those things up effectively like a zip so connect the pieces because in this case they also have the same account we don't have to but you know in this case they do and i can say names dot zip ages and what this will give me if i debug this it's actually a very handy feature in fact let me remove the explicit type again so you can see what's going on here this will be the names or the name and this will be the age what it will do is like i said zip the two enumerables and give me back an enumerable of a tuple and as you can see in this iterator it combined nick with 28 peter with 22 maria with 25. nice thing it was in link already what we can do now is also have a third thing here so if i was to have something like years of experience and i want to add that as well i can now do that and change this to end years of exp here we go and this is now this can happen so it's a small enhancement of the previous api which only accepted two now as you can see here i totally screwed this up as you can see here now we have three things we can zip together a small enhancement still not something i would use on the daily but definitely something i can see coming in handy when i'm doing some you know small console application to process some data in memory from a file or something so next is another very handy one and it is the buy operators in some existing methods so what does that mean let's say we have an array of family members here right what if i wanted to get the youngest person in the family and then the oldest person in the family with 3.06 link what i would have to do is to say youngest year equals family dot order by an age and then get the first and if i want the oldest i would say again family i can just copy that and just change this and then say order by descending and if i do that as you can see here the youngest is peter the oldest is me at 28. so that's how you would do that before and the thing is this is one link methods too many i'd say for what we want to do so what we can do now with the new api is we can use the min by and max by so let's say that this is the old method of doing things so old and old and then in the new wave of things i can say min by and now this accepts an expression and it will give me back the minimum by age and then here i'm gonna say max buy same thing and if i debug this i can simply get that back so youngest peter oldest me very nice we don't have to call that extra our operation very concise makes sense very handy and it's good that they actually did this for other methods as well we now have distinct by intersect by union buy and accept buy so same api you can accept an expression and do something with them now the next two fall into this modernization category that i said where we're just catching up with the c-sharp 8 9 and 10 features and in this case we have index support for element add so if i want to get an element at a specific index i could say element at a specific location let's say 3 here and this would give me the third thing in the third index not the third thing so the fourth thing uh which is layla in this case i can say element at however c sharp eight added um index operators so if i wanted let's say to get the third item from the end you know how would i do that in the old way i'd have to try to get the index and blah blah blah would be a pain in the uh where you don't want to have a pain so instead what we can do now is we can use the index operator and say third from the end and this if i debug it will give me layla again here we go so third from the end with this operator and if you're not caught up with the index operators microsoft has great documentation on the matter i highly recommend you check that out it is a bit of a weird syntax if you've never been exposed to it before but once you understand what this symbol means you'll be able to sort it out very very easily and now in sort of like the common theme here we have range support for take so previously let's say that i want to get a slice here let's say i get john and layla what i would say is names dot skip two and then take two and what this would give me is it would skip the two first in this case and then give me two so john and linda if i just quickly debug this for you to see if i step over this as you can see john and layla here now again this is one link called too many so microsoft said that you know what you don't have to do this what you can say is two 2 4 which is what you want so this is the range syntax and now if i debug this this will give me the exact same experience john layla so it is from 2 to four now you can also mix and match things like index support so if i for example wanna say give me the last three i can write it like this so last three equals names dot take third from the end until the end so this will give me the last three again if you see the syntax and you're like what the hell is that it's a bit of a weird one if you've never been exposed to it but now i got the last three again i'm going to link to documentation for both index and range below check them out they're used in many places it would be a very good thing for you to actually catch up with them that's all i have for you for this video thank you very much for watching special next my patreons for making videos possible if you want to support me as well you're going to find link in the description down below leave a like if you like this video subscribe smoking like this and ring the bell as well and i'll see you in the next video keep coding you
Info
Channel: Nick Chapsas
Views: 115,856
Rating: undefined out of 5
Keywords: Elfocrash, elfo, coding, .netcore, dot net, core, C#, how to code, tutorial, development, software engineering, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, clean code, dotnet, .NET 6, c# 10, c# 8, linq, linq in c#, linq improvements, linq performance, new linq methods in .net 6, 7 awesome improvements for LINQ in .NET 6
Id: sIXKpyhxHR8
Channel Id: undefined
Length: 14min 32sec (872 seconds)
Published: Thu Oct 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.