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

Поділитися
Вставка
  • Опубліковано 15 лис 2024

КОМЕНТАРІ • 73

  • @Vennotius
    @Vennotius 2 роки тому +11

    Thank you for this. This is much quicker than reading through the detailed blog posts (which have their place of course).

  • @saadablat2998
    @saadablat2998 Рік тому +2

    Thanks a lot! Best teacher ever!

  • @mtranchi
    @mtranchi 2 роки тому +5

    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.

  • @caseyspaulding
    @caseyspaulding 2 роки тому +16

    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  2 роки тому +4

      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".

  • @ImmoLandwerth
    @ImmoLandwerth 2 роки тому

    So glad to see folks like you getting excited about a feature I proposed ❤🎉

    • @IAmTimCorey
      @IAmTimCorey  2 роки тому

      Thanks for getting it added. It is a nice option to have.

  • @cadmus777
    @cadmus777 2 роки тому +6

    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.

  • @benjaminshinar6235
    @benjaminshinar6235 2 роки тому +2

    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  2 роки тому

      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.

  • @kacperwyczawski618
    @kacperwyczawski618 2 роки тому +4

    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  2 роки тому +1

      Thanks for sharing! I updated the video.

  • @expertreviews1112
    @expertreviews1112 2 роки тому +1

    Top drawer content

  • @Diegos79
    @Diegos79 2 роки тому +3

    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 2 роки тому

      u find out yet?

    • @Diegos79
      @Diegos79 2 роки тому

      @@chezchezchezchez no...

    • @Tsunami14
      @Tsunami14 2 роки тому +1

      I'd guess that sort is slower since it updates the underlying collection?

    • @ryanhaney
      @ryanhaney 2 роки тому +2

      OrderBy is part of the fluent linq api. Since linq operates on IEnumerables, it is immutable. Sort mutates the source array / list.

    • @RobinHood70
      @RobinHood70 Рік тому +1

      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.

  • @sarfraz8533
    @sarfraz8533 2 роки тому

    Nice , 10min video series is 👌

  • @Stabruder
    @Stabruder 2 роки тому +2

    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  2 роки тому +2

      Good question. I don't know the answer. My assumption is that they will be at least very similar.

    • @IAmTimCorey
      @IAmTimCorey  2 роки тому +1

      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 2 роки тому +1

      @@IAmTimCorey Thank you really much! Interested me a lot honestly

  • @АндрейМещенок
    @АндрейМещенок 2 роки тому +3

    Спасибо за урок по LINQ

  • @selahattinkaradogan2744
    @selahattinkaradogan2744 2 роки тому

    THNX tim useful video and short .Sorting Algorithm

  • @astralpowers
    @astralpowers 2 роки тому +1

    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 2 роки тому +1

      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.

  • @zacky7862
    @zacky7862 2 роки тому

    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  2 роки тому

      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/

  • @williamlebron2078
    @williamlebron2078 2 роки тому +1

    Great video

  • @iosmany_cu6932
    @iosmany_cu6932 2 роки тому +2

    As always thanks for your work! Awesome job

  • @magicja
    @magicja 2 роки тому

    Are there performance differences between the two?

    • @IAmTimCorey
      @IAmTimCorey  2 роки тому

      Yes, there is a slight performance improvement.

  • @S3Kglitches
    @S3Kglitches 2 роки тому

    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  2 роки тому

      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.

  • @chris5947
    @chris5947 Рік тому

    cool, thank you!

  • @Tsunami14
    @Tsunami14 2 роки тому

    Why didn't they just make Order() extend on IEnumerable to catch the error at compile time?

    • @IAmTimCorey
      @IAmTimCorey  2 роки тому

      I'm not sure.

    • @Tsunami14
      @Tsunami14 2 роки тому

      @@IAmTimCorey Fair enough. I appreciate the response.

  • @waynehawkins654
    @waynehawkins654 2 роки тому

    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  2 роки тому +1

      There is a slight speed difference (improvement). Each now has a distinct use-case.

  • @makariyu69
    @makariyu69 2 роки тому

    is there an equivalent of this with java? Great stuff!

  • @Kaalund1989
    @Kaalund1989 2 роки тому +1

    Hello Tim any chance you want to explain about cancel. tokens and how to use it correct. if possible in blazor server :)

    • @IAmTimCorey
      @IAmTimCorey  2 роки тому +1

      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/

  • @Songfugel
    @Songfugel 2 роки тому +3

    Insane how this wasn't implemented from the beginning

    • @IAmTimCorey
      @IAmTimCorey  2 роки тому +3

      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 2 роки тому +1

      @@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

  • @mertkurt947
    @mertkurt947 Рік тому

    thank you =)

  • @alexcosta2614
    @alexcosta2614 2 роки тому

    Soon c# will allow you to build entire systems with 20 lines of code.

  • @SaveTheHedgehog
    @SaveTheHedgehog 2 роки тому

    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  2 роки тому +2

      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.

  • @powermetal1963
    @powermetal1963 2 роки тому

    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  2 роки тому

      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

  • @keyboard_g
    @keyboard_g 2 роки тому

    This feature really feels like unnecessary language bloat.

    • @IAmTimCorey
      @IAmTimCorey  2 роки тому

      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  Рік тому +1

      They can use either. One is just simpler than the other. I'm not sure what confusion it would cause.