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

  Рет қаралды 22,860

Milan Jovanović

Milan Jovanović

Күн бұрын

Пікірлер
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Want to master Clean Architecture? Go here: bit.ly/3PupkOJ Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
@addkim7889
@addkim7889 Жыл бұрын
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.
@JavierAcrich
@JavierAcrich Жыл бұрын
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 Жыл бұрын
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. 🤷‍♂️ 🥔🥔🍅🍅
@majora2007-joe
@majora2007-joe Жыл бұрын
Great tip, I usually put if statements in my repo, but this makes me realize I'm under utilizing extensions yet again.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Extension methods are awesome 😁
@pilotboba
@pilotboba Жыл бұрын
I always compose my queries this way, but adding an extension method is a great tip.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Awesome, glad you liked it :)
@drozdgabriel
@drozdgabriel Жыл бұрын
I always learn new things with your videos 😄 Always a new perspective on things that are familiar to me. It's great! 😅
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
That's awesome to hear! Thank you :)
@MortvmMM
@MortvmMM Жыл бұрын
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 Жыл бұрын
There's no need to do AsQueryable if you get the Queryable directly from the DbContext
@winchester2581
@winchester2581 Жыл бұрын
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 Жыл бұрын
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 :)
@NAngelGr
@NAngelGr Жыл бұрын
You are a star mate!! Thanks for your tutorials!!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Happy to help
@mocococo2877
@mocococo2877 Жыл бұрын
Thank you and again greetings from Bulgaria. As per your instruction and per my liking I smashed the like button.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Awesome, thank you! 💪😁
@NavedDeshmukh
@NavedDeshmukh Жыл бұрын
Great approach. such simple tweak but adds so much clarity to code
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I only recently found out about this, but it's really helpful
@mohamed-hassan-
@mohamed-hassan- Жыл бұрын
i used it in various queries, thanks for the video 👏
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Nice! I knew I can't be the only one 😁
@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.
@MilanJovanovicTech
@MilanJovanovicTech 9 ай бұрын
Glad it was helpful!
Жыл бұрын
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 Жыл бұрын
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.
@nepalxplorer
@nepalxplorer Жыл бұрын
Very useful, adding it on my project 😛
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Great!
@mylesdavies9476
@mylesdavies9476 Жыл бұрын
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 Жыл бұрын
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 😁
@ayasaber2567
@ayasaber2567 5 ай бұрын
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 5 ай бұрын
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
@Mohamad-jz1qz
@Mohamad-jz1qz Жыл бұрын
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.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
It's still a filter in the DB
@arunbm123
@arunbm123 Жыл бұрын
another brilliant tutorial !!!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thanks!
@nico11323
@nico11323 Жыл бұрын
Hi, there is a way to remove the check empty in the generate sql query?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Nope, that's how EF Core translates it to SQL
@majormartintibor
@majormartintibor Жыл бұрын
Good one Milan, thanks!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Glad you liked it!
@Tamer_Ali
@Tamer_Ali Жыл бұрын
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
@fitstrong167
@fitstrong167 Жыл бұрын
Just a random suport comment .... great staff keep em coming ... ajde :)
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
You're the best!
@codingbloke
@codingbloke Жыл бұрын
Very nice bit of technique.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thank you, sir!
@tsatsosgregorian9410
@tsatsosgregorian9410 Жыл бұрын
Hello Milan, great video again!! Any good advice or video for efcore bulk insert?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Not really, I would consider SqlBulkCopy
@hassanOnYoutube
@hassanOnYoutube Жыл бұрын
Love from Pakistan ! love that extension part.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thanks a lot, Hassan! :)
@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
@리오날정
@리오날정 Жыл бұрын
Thanks Milan😊
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
You're very welcome :)
@AhmedKhaled-g8t
@AhmedKhaled-g8t Жыл бұрын
Amazing channel
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thank you very much!
@volodymyrliashenko1024
@volodymyrliashenko1024 Жыл бұрын
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.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
In SQL 😁😁
@volodymyrliashenko1024
@volodymyrliashenko1024 Жыл бұрын
@@MilanJovanovicTech the response is too short :) Would be nice to see like an example.
@waleedbensumaidea3947
@waleedbensumaidea3947 Жыл бұрын
You're amazing
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thanks!
@ahmedh2482
@ahmedh2482 Жыл бұрын
U are awesome man
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thanks a lot! :)
@joehernandez3231
@joehernandez3231 Жыл бұрын
This is great. I just added this WhereIf extension method to a new API I started working on. Thanks!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Awesome, glad you found it practical 😁
@Tamer_Ali
@Tamer_Ali Жыл бұрын
Thanks Milan, you always amazing me 👍.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Happy to be of help 😁
@VishnuKumar-fq1zo
@VishnuKumar-fq1zo Жыл бұрын
How did you managed to print actual query in the console?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
There's a method to configure EF output
@VishnuKumar-fq1zo
@VishnuKumar-fq1zo Жыл бұрын
@@MilanJovanovicTech Can share code for doing that?
@meirkr
@meirkr Жыл бұрын
Does OData take care of empty filter internally?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I have no idea
@TheSead750
@TheSead750 Жыл бұрын
very nice😄
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thanks! Got any EF extension method of your own?
@andresjimenez3140
@andresjimenez3140 Жыл бұрын
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.
@Alupl
@Alupl Жыл бұрын
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 Жыл бұрын
I assume it's not added because you can achieve the same using just Where
@mhar5838
@mhar5838 10 ай бұрын
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 10 ай бұрын
Wherever you'd need them to write LINQ queries
@mhar5838
@mhar5838 10 ай бұрын
@@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?
@krunalsisodiya
@krunalsisodiya Жыл бұрын
I find it very helpful
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Glad it was helpful!
@haythambiadse5137
@haythambiadse5137 Жыл бұрын
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 :)
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Won't have much effect on performance
@KunalMukherjee3701
@KunalMukherjee3701 Жыл бұрын
Interesting 🤔
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Indeed 🤔
@anime-dx4tz
@anime-dx4tz Жыл бұрын
project git link please
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
www.patreon.com/milanjovanovic
@delphiguy23
@delphiguy23 Жыл бұрын
Nice
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thanks!
@techpc5453
@techpc5453 Жыл бұрын
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
🌕
Mapping Domain-Driven Design Concepts To The Database With EF Core
18:06
Milan Jovanović
Рет қаралды 55 М.
How To Optimize EF Core Query Performance With Compiled Queries
12:14
Milan Jovanović
Рет қаралды 18 М.
Chain Game Strong ⛓️
00:21
Anwar Jibawi
Рет қаралды 41 МЛН
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 30 МЛН
Сестра обхитрила!
00:17
Victoria Portfolio
Рет қаралды 958 М.
Understand your C# queries! IEnumerable & IQueryable in explained
11:28
tutorialsEU - C#
Рет қаралды 40 М.
How I Use The Generic Repository Pattern In Clean Architecture
17:15
Milan Jovanović
Рет қаралды 41 М.
How do Entity Framework Core Queries Work?
28:44
Raw Coding
Рет қаралды 27 М.
Real 10x Programmers Are SLOW To Write Code
14:51
Thriving Technologist
Рет қаралды 68 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 900 М.
Will This New EF Core Feature Be The End Of Dapper?
14:11
Milan Jovanović
Рет қаралды 56 М.
Event-Driven Architecture (EDA) vs Request/Response (RR)
12:00
Confluent
Рет қаралды 177 М.
Solving one of PostgreSQL's biggest weaknesses.
17:12
Dreams of Code
Рет қаралды 220 М.
EF Core Performance Optimization Challenge | 233x FASTER
14:42
Milan Jovanović
Рет қаралды 70 М.
Using Multiple EF Core DbContexts in a Single Application
17:46
Milan Jovanović
Рет қаралды 38 М.
Chain Game Strong ⛓️
00:21
Anwar Jibawi
Рет қаралды 41 МЛН