.NET 7 Update: Simplified LINQ Ordering In 10 Minutes or Less

  Рет қаралды 25,865

IAmTimCorey

IAmTimCorey

Күн бұрын

In .NET 7, LINQ now has simplified ordering of values. Let's see it in action in this 10-Minute Training video.
Full Training Courses: IAmTimCorey.com
Source Code: leadmagnets.app/?Resource=Sim...
0:00 Intro
0:21 Introduction to code
0:50 Old way of ordering
1:36 New way of ordering
1:57 Testing
2:23 Ordering complex objects
5:28 Order descending
6:13 Outro

Пікірлер: 73
@Vennotius
@Vennotius Жыл бұрын
Thank you for this. This is much quicker than reading through the detailed blog posts (which have their place of course).
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@caseyspaulding
@caseyspaulding Жыл бұрын
Stuff like this is GREAT for continuing education. As a beginner I feel like I am jumping into a fast moving river 😂 I just need to go do a few of your courses and not do anything else for awhile to get basics down I think
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Yep, focusing on learning the language instead of trying to keep up with the latest changes will be helpful. Not only will it give you a more solid target to hit, but it will also teach you how to deal with previous versions of .NET. For instance, in my C# Mastercourse, I teach .NET 6, but I also teach the .NET Framework. That's because you are going to see and have to work with the .NET Framework in "the real world".
@email195
@email195 Жыл бұрын
I just built an app in .net6 and used OrderBy on a list of string and wondered why there isn’t an easier way. It’s like they read my mind! Thanks for sharing this little tidbit.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Excellent!
@mtranchi
@mtranchi Жыл бұрын
the problem with ordering complex objects with using .OrderBy(x => x.Whatever) is that it creates a new object on the heap, thus adding to the strain on the garbage collector. the best way to attack the issue of ordering complex objects is to put static methods on the class such as "SortByAge", "SortByName" that returns an int. That way it morphs the actual IEnumerable in place rather than creating a new object on the heap. sure, i know functional programming's mantra and the side effects of changing the underlying model, but let's be real: Sometimes you want to give the user the ability to sort by name, by age, by country, by... a whole host of properties, and each time they click a button to re-order, it's a waste of memory allocations on the heap to create a new IEnumerable from the existing List or whatever. better and more efficient to sort in place.
@cadmus777
@cadmus777 Жыл бұрын
I really hope there are some more cool changes to LINQ, as this is pretty small! Thanks though, I wouldn't know about it if not for you.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@ImmoLandwerth
@ImmoLandwerth Жыл бұрын
So glad to see folks like you getting excited about a feature I proposed ❤🎉
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thanks for getting it added. It is a nice option to have.
@saadablat2998
@saadablat2998 Жыл бұрын
Thanks a lot! Best teacher ever!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@selahattinkaradogan2744
@selahattinkaradogan2744 Жыл бұрын
THNX tim useful video and short .Sorting Algorithm
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@sarfraz8533
@sarfraz8533 Жыл бұрын
Nice , 10min video series is 👌
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@kacperwyczawski618
@kacperwyczawski618 Жыл бұрын
0:00 Intro 0:21 Introduction to code 0:50 Old way of ordering 1:36 New way of ordering 1:57 Testing 2:23 Ordering complex objects 5:28 Order descending 6:13 Outro
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thanks for sharing! I updated the video.
@williamlebron2078
@williamlebron2078 Жыл бұрын
Great video
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thanks!
@benjaminshinar6235
@benjaminshinar6235 Жыл бұрын
3:30 why is this a runtime exception? shouldn't the compiler stop you from calling order on something which isn't Incomparable?
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Good question. My guess is that it will become a compiler exception in .NET 8, but I don't know for sure. There might be more to it than what I'm seeing.
@astralpowers
@astralpowers Жыл бұрын
It's strange that they didn't add a generic type parameter constraint to require the element type implements IComparable for the Order and OrderDescending methods.
@Tsunami14
@Tsunami14 Жыл бұрын
Just posted the exact same question/observation. I see no reason to not do it that way, and you'd catch the error at compile time vs run time.
@chris5947
@chris5947 Жыл бұрын
cool, thank you!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@Diegos79
@Diegos79 Жыл бұрын
Sorry for stupid question, but what about performance differences between Sort( ) and Order ( ) ? When use Sort ( ) and when Order ( ) ? Thanks a lot for your stunning educational videos!
@chezchezchezchez
@chezchezchezchez Жыл бұрын
u find out yet?
@Diegos79
@Diegos79 Жыл бұрын
@@chezchezchezchez no...
@Tsunami14
@Tsunami14 Жыл бұрын
I'd guess that sort is slower since it updates the underlying collection?
@ryanhaney
@ryanhaney Жыл бұрын
OrderBy is part of the fluent linq api. Since linq operates on IEnumerables, it is immutable. Sort mutates the source array / list.
@RobinHood70
@RobinHood70 Жыл бұрын
It depends what you're sorting and how many of them there are. My experience is that for smaller lists, Sort is typically faster by a couple of percent, but on larger lists, OrderBy takes the lead. There are a lot of potential factors that go into that, though, so if you're worried about that extra couple of percent and you don't care whether it's an in-place sort vs. a separate copy, the best bet is to do timing tests on typical data and see what works best. Another factor to be aware of is that OrderBy produces what's called a "stable" sort...in other words, if two items compare the same, the one that came first in the original data will come first in the result. Sort doesn't guarantee that. My information might be out of date, though...v7 made a lot of improvements in some areas and this might be one of them. Either way, timing it in your particular use case is always going to be your best option.
@zacky7862
@zacky7862 Жыл бұрын
Please do tutorial for understanding and right usage of Native AOT. I can only use AOT for simple project but for complex such as JSON Serializing/Deserializing it doesn't work.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/
@user-pn8pz1od1x
@user-pn8pz1od1x Жыл бұрын
Спасибо за урок по LINQ
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@mertkurt947
@mertkurt947 Жыл бұрын
thank you =)
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@iosmany_cu6932
@iosmany_cu6932 Жыл бұрын
As always thanks for your work! Awesome job
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@expertreviews1112
@expertreviews1112 Жыл бұрын
Top drawer content
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@Stabruder
@Stabruder Жыл бұрын
Is there any performance difference when using any of these two, or is it just lowered by the compiler to the same source code?
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Good question. I don't know the answer. My assumption is that they will be at least very similar.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
OK, so I did some research and there is a bit of a benefit for using Order as opposed to OrderBy. Here is the post with more information (at the bottom of the LINQ section): devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/#linq
@Stabruder
@Stabruder Жыл бұрын
@@IAmTimCorey Thank you really much! Interested me a lot honestly
@makariyu69
@makariyu69 Жыл бұрын
is there an equivalent of this with java? Great stuff!
@asdfxyz_randomname2133
@asdfxyz_randomname2133 Жыл бұрын
java streams is an equivalent to linq
@makariyu69
@makariyu69 Жыл бұрын
@@asdfxyz_randomname2133 thanks!
@Tsunami14
@Tsunami14 Жыл бұрын
Why didn't they just make Order() extend on IEnumerable to catch the error at compile time?
@IAmTimCorey
@IAmTimCorey Жыл бұрын
I'm not sure.
@Tsunami14
@Tsunami14 Жыл бұрын
@@IAmTimCorey Fair enough. I appreciate the response.
@Songfugel
@Songfugel Жыл бұрын
Insane how this wasn't implemented from the beginning
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Everything takes effort. This is no exception. This was actually a tricky fix, and when they already had a functional and fairly easy-to-use solution, it wasn't deemed a high priority.
@Songfugel
@Songfugel Жыл бұрын
@@IAmTimCorey Maybe there is some underlying optimization/implementation hurdle I am not understanding then. I still remember being so frustrated by the lack of simple default ordering function when LINQ first came out almost 2 decades ago. I would have also made ordering of complex types default to ordering them by declaration order unless specifically defined with orderby
@waynehawkins654
@waynehawkins654 Жыл бұрын
Nice but with Visual Studio AI learning, it's a double click of the 'Tab' key to write the code anyway. If it runs the same speed in a loop, then what is the point. Just another thing to rember in my view.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
There is a slight speed difference (improvement). Each now has a distinct use-case.
@S3Kglitches
@S3Kglitches Жыл бұрын
Why doesn't it give compile-time error for Person object that it doesn't support IComparable? I expect compile errors in C# for this.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
My guess is that this will come later on. Compile-time errors don't just happen - they need to be developed. In order to do that, you need to have the code to test them on. So I think it will come, unless there is something I missed in terms of complexity.
@magicja
@magicja Жыл бұрын
Are there performance differences between the two?
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Yes, there is a slight performance improvement.
@alexcosta2614
@alexcosta2614 Жыл бұрын
Soon c# will allow you to build entire systems with 20 lines of code.
@powermetal1963
@powermetal1963 Жыл бұрын
I don't see why this is promoted as a new way of ordering in LINQ, as it is simple sugar method shortcuts for the corresponding `OrderBy{Descending}(x => x)` overloads. No less allocations at all. No performance gains. Just improved readability. Anyone could have created such custom extension methods in less than 5 min, no need to wait 10+ years for .NET7 to get such "new functionality" out of the box.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Because it can make some assumptions about your data, Order actually has less primary allocations for your data, resulting in better performance than OrderBy in the same scenario. Here is a link with more details: devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/#linq
@SaveTheHedgehog
@SaveTheHedgehog Жыл бұрын
Why do they make things unnecessary complex? Orderby does do it's function and you don't have to think about complex values. I'm not happy with the way the. Net team is changing.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
It feels like you are arguing that "OrderBy(x => x);" is less complex than "Order();" What I showed in this video is that Order works better for simple types and OrderBy works better for complex types. Now you have an option. If you don't want to use the new option, you don't have to. OrderBy isn't going anywhere.
@keyboard_g
@keyboard_g Жыл бұрын
This feature really feels like unnecessary language bloat.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Why? It is a simplified syntax that actually saves some memory pressure as well.
@babyfool
@babyfool Жыл бұрын
seems like the most useless feature. ppl will be confused between order and sort.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
They can use either. One is just simpler than the other. I'm not sure what confusion it would cause.
@Kaalund1989
@Kaalund1989 Жыл бұрын
Hello Tim any chance you want to explain about cancel. tokens and how to use it correct. if possible in blazor server :)
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/
.NET 7 Update: Nullable Models in MVC in 10 Minutes or Less
5:01
.NET 7 Update: Required Properties in 10 Minutes or Less
9:50
IAmTimCorey
Рет қаралды 13 М.
How many pencils can hold me up?
00:40
A4
Рет қаралды 19 МЛН
He tried to save his parking spot, instant karma
00:28
Zach King
Рет қаралды 19 МЛН
Как быстро замутить ЭлектроСамокат
00:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 12 МЛН
.NET Core vs .NET Framework - What's the difference?
25:26
IAmTimCorey
Рет қаралды 641 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 251 М.
C# Events | Events in C# with example | C# interview question
20:31
Code With RaiGenics
Рет қаралды 15 М.
.NET 7 Update: List Patterns in 10 Minutes or Less
10:00
IAmTimCorey
Рет қаралды 21 М.
The Dictionary Data Structure in C# in 10 Minutes or Less
10:20
IAmTimCorey
Рет қаралды 29 М.
10 C# Libraries To Save You Time And Energy
33:59
IAmTimCorey
Рет қаралды 204 М.