One LINQ Extension Method You NEED To Know For Cleaner EF Core Queries

Поделиться
HTML-код
  • Опубликовано: 23 ноя 2024

Комментарии • 104

  • @MilanJovanovicTech
    @MilanJovanovicTech  Год назад +1

    Want to master Clean Architecture? Go here: bit.ly/3PupkOJ
    Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt

  • @addkim7889
    @addkim7889 Год назад +2

    Really a clever way to apply condition, making the input as a Func instead of bool would make it more adaptable, plus the Expression Api is used for Linq, and EfCore, and other ORM, used that to compose the actual query.

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад

      Func would just add complexity since it would always be () => somethingThatReturnBool;
      What's the advantage over returning a bool directly?

    • @addkim7889
      @addkim7889 Год назад

      @@MilanJovanovicTech you are right that it would be more complicated, for more complicated filtering that would be cleaner if extracted into a function.

  • @majora2007-joe
    @majora2007-joe Год назад +3

    Great tip, I usually put if statements in my repo, but this makes me realize I'm under utilizing extensions yet again.

  • @JavierAcrich
    @JavierAcrich Год назад +19

    If I call the extension method WhereIf, I would have the predicate as a first parameter and the condition second. Otherwise I would call it 'IfWhere'. So it has concordance with the parameter order.

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад +5

      Good point, honestly. Haven't thought about it!

    • @Knusperfunk
      @Knusperfunk Год назад

      I originally had it the other way around, but having the conditions as the first parameter makes them nicely line up when chaining a lot of filters, which gives a slightly better overview. 🤷‍♂️ 🥔🥔🍅🍅

  • @pilotboba
    @pilotboba Год назад +2

    I always compose my queries this way, but adding an extension method is a great tip.

  • @MortvmMM
    @MortvmMM Год назад +3

    Nice extension method :) A nice fluent alternative to building your queries. The basic way of doing it is marking your query AsQueryable and adding to it like if (condition) query = query.Where()

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад +1

      There's no need to do AsQueryable if you get the Queryable directly from the DbContext

  • @drozdgabriel
    @drozdgabriel Год назад +7

    I always learn new things with your videos 😄
    Always a new perspective on things that are familiar to me.
    It's great! 😅

  • @winchester2581
    @winchester2581 Год назад +2

    Simple, but awesome and useful video, used this approach on one of the previous projects :)
    Also when I worked there, I've got a hint of not using Where statement in the loop, 'cuz it's gonna generate a lot of queries which is not really convenient in terms of performance, so we used LinqKit for that (to combine specific filters to apply to query)
    So I think that such videos really helps developers to write LINQ expressions wisely, my kudos)

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад +1

      I'll have to research about LinqKit 🤔🤔

    • @winchester2581
      @winchester2581 Год назад

      @@MilanJovanovicTech great! I guess that we can just write our own extension methods with the same logic but I used it as it was introduced in our project :)

  • @88spaces
    @88spaces 9 месяцев назад

    I like the extension method. I might use it soon. You've also given me ideas about how to reduce code using extension methods and EF. Thanks.

  • @mocococo2877
    @mocococo2877 Год назад +1

    Thank you and again greetings from Bulgaria.
    As per your instruction and per my liking I smashed the like button.

  • @hassanOnYoutube
    @hassanOnYoutube Год назад +1

    Love from Pakistan ! love that extension part.

  • @mylesdavies9476
    @mylesdavies9476 Год назад +3

    Hi Milan, really enjoyed your tech talk on modular monolith. It would be great if you could do a video outlining the basic building blocks, i.e. your preferred approach for bringing the modules together and explaining some of the core concepts

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад +1

      Oh I'm planning to 😁 In fact, more than one video. And I'll even be open sourcing an existing implementation of mine.

    • @mylesdavies9476
      @mylesdavies9476 Год назад

      @@MilanJovanovicTech awesome, look forward to it 😁

  •  Год назад +2

    Interesting approach. As every permutation of conditions in code would generate new SQL, and therefor new query plans... that again could impact performance. It would be interesting to hear a DBAs view on this. Might be a good thing, and might be a bad thing. I am actually not sure.

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад +1

      How is the query plan cache? 🤔 My thinking is if these queries are frequently ran anyway, they would constantly be in the cache regardless. Although your concern is perfectly valid.

  • @NavedDeshmukh
    @NavedDeshmukh Год назад +1

    Great approach. such simple tweak but adds so much clarity to code

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад +1

      I only recently found out about this, but it's really helpful

  • @mohamed-hassan-
    @mohamed-hassan- Год назад +1

    i used it in various queries, thanks for the video 👏

  • @NAngelGr
    @NAngelGr 11 месяцев назад +1

    You are a star mate!! Thanks for your tutorials!!

  • @nepalxplorer
    @nepalxplorer Год назад +2

    Very useful, adding it on my project 😛

  • @tsatsosgregorian9410
    @tsatsosgregorian9410 Год назад +1

    Hello Milan, great video again!!
    Any good advice or video for efcore bulk insert?

  • @codingbloke
    @codingbloke Год назад +1

    Very nice bit of technique.

  • @volodymyrliashenko1024
    @volodymyrliashenko1024 Год назад +1

    I'm a big fun of this extension method and I have different versions of it
    Wondering, how devs who like Dapper implement such logic.

  • @Mohamad-jz1qz
    @Mohamad-jz1qz 11 месяцев назад +1

    Hi, I want to know that if we have a large amount of data, this approach won't be failed? because firstly your are going to select all and then filter outside of sql. Thanks in advanced.

  • @arunbm123
    @arunbm123 Год назад +1

    another brilliant tutorial !!!

  • @nico11323
    @nico11323 Год назад +2

    Hi, there is a way to remove the check empty in the generate sql query?

  • @andresjimenez3140
    @andresjimenez3140 Год назад +1

    Great content !!!
    One question, where would you implement this extension method in a clean architecture? Is it in infrastructure layer?

    • @ugochukwuumerie6378
      @ugochukwuumerie6378 Год назад

      Exactly, cause that's a reasonable place to do that. Ef core itself goes in there.☺️

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад

      I guess so, yeah. Or Persistence - if you have a separate layer for that.

  • @Tamer_Ali
    @Tamer_Ali Год назад +2

    Is there a better approach (generic) to filter data from 2 lists to save to database?
    e.g:
    list1 is the existing users from database
    list2 is the users that the admin want to add, update or delete after comparing with the existing users in the database

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад

      Not that I know of, honestly 🤔

    • @stefan-d.grigorescu
      @stefan-d.grigorescu Год назад

      Convert both to HashSets if you can have all the entries in memory, then use set1.Exclude(set2) or something like that.
      If the persisted entities are too many to be worth it loading them all into memory at once, you have to check foreach item in list1 if(dbSet2.FirstOrDefault...). Also consider checking the local cache of dbContext before checking the DbSet
      That's all know for now about this. Choosing one approach or another should be based on some testing. Or just nvm and keep it simple if it already performs ok

  • @리오날정
    @리오날정 Год назад +1

    Thanks Milan😊

  • @ayasaber2567
    @ayasaber2567 4 месяца назад

    Thanks for the explanation,
    'where' part will be ignored (incase the filter variable are null or empty)
    in the statement whether we use the .Where twice or use the extension method, so what's the benefit from the extension method rather than reusability?

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 месяца назад

      From my perspective, it makes it easier to compose multiple optional conditions.
      Imagine you want to have 5-10 optional filters that we want to apply only if the filter is specified by the user.
      It could be one big WHERE statement, with null check + condition
      -or-
      It could be many WHERE statement with null check + condition
      -or-
      Using the approach presented here to make the above a bit easier for the developer

  • @majormartintibor
    @majormartintibor Год назад +1

    Good one Milan, thanks!

  • @haythambiadse5137
    @haythambiadse5137 Год назад +1

    can you think of situations it could help to improve performance?
    also ,can you please do a video for queries that runs inside a loop ... ? i bet you have great tips over this :)

  • @joehernandez3231
    @joehernandez3231 Год назад

    This is great. I just added this WhereIf extension method to a new API I started working on. Thanks!

  • @AhmedKhaled-g8t
    @AhmedKhaled-g8t Год назад +1

    Amazing channel

  • @mhar5838
    @mhar5838 9 месяцев назад

    Hey Milan
    thanks for the video - just a question: in clean code architecture which project/folder do you suggest to put such extensions? Domain? Application? Persistance?

    • @MilanJovanovicTech
      @MilanJovanovicTech  9 месяцев назад

      Wherever you'd need them to write LINQ queries

    • @mhar5838
      @mhar5838 9 месяцев назад

      @@MilanJovanovicTech I said where not when! I mean do you put the extension method classes in domain project or persistance project? sometimes you may want to write some business logic in domain / application layer. but some people recommend to put Queryable operations in repository or persistance layer. What is your recommendation?

  • @waleedbensumaidea3947
    @waleedbensumaidea3947 Год назад +1

    You're amazing

  • @Tamer_Ali
    @Tamer_Ali Год назад +1

    Thanks Milan, you always amazing me 👍.

  • @Alupl
    @Alupl 11 месяцев назад

    I'm using this Extension method since .NET Framework 4 :D Today in .NET Core 8.0 I'm surprised there still isn't such build in method (or I don't know of it). Funny thing is that very few programmers know this but as you showed in your video, It's so much simpler and effective. I don't get it why this isn't a standard ... Anyway thanks for your vid - I didn't remember the syntax and came here :P

    • @MilanJovanovicTech
      @MilanJovanovicTech  11 месяцев назад

      I assume it's not added because you can achieve the same using just Where

  • @fitstrong167
    @fitstrong167 Год назад +1

    Just a random suport comment .... great staff keep em coming ... ajde :)

  • @VishnuKumar-fq1zo
    @VishnuKumar-fq1zo Год назад +1

    How did you managed to print actual query in the console?

  • @ahmedh2482
    @ahmedh2482 Год назад +1

    U are awesome man

  • @WahidRezgui
    @WahidRezgui Год назад

    Hello first of all thanks for the video . Ihave one question do you have any idea how to make the operator like =,>,< generic and selected by user like the sample you provided Age =10 or age>10

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад

      Generic in what way?

    • @WahidRezgui
      @WahidRezgui Год назад

      I meant the operator also is given by the user so searchvalue, operator and field are given as parameter something like this var res= dbcontext.entity.where(x=>x.fieldA operator searchedvalue)
      So here the search is done on fieldA and also operator is a property set by the function so finally the searchfunction will take fieldA . Operator and searched value as a parameter

  • @krunalsisodiya
    @krunalsisodiya Год назад +1

    I find it very helpful

  • @TheSead750
    @TheSead750 Год назад +2

    very nice😄

  • @meirkr
    @meirkr Год назад +1

    Does OData take care of empty filter internally?

  • @KunalMukherjee3701
    @KunalMukherjee3701 Год назад +2

    Interesting 🤔

  • @delphiguy23
    @delphiguy23 Год назад +1

    Nice

  • @anime-dx4tz
    @anime-dx4tz Год назад +1

    project git link please

  • @techpc5453
    @techpc5453 Год назад +1