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 you are right that it would be more complicated, for more complicated filtering that would be cleaner if extracted into a function.
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.
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. 🤷♂️ 🥔🥔🍅🍅
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()
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 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 :)
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 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.
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.
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.
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
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
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?
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
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 :)
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 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?
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
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
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
Want to master Clean Architecture? Go here: bit.ly/3PupkOJ
Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
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.
Func would just add complexity since it would always be () => somethingThatReturnBool;
What's the advantage over returning a bool directly?
@@MilanJovanovicTech you are right that it would be more complicated, for more complicated filtering that would be cleaner if extracted into a function.
Great tip, I usually put if statements in my repo, but this makes me realize I'm under utilizing extensions yet again.
Extension methods are awesome 😁
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.
Good point, honestly. Haven't thought about it!
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. 🤷♂️ 🥔🥔🍅🍅
I always compose my queries this way, but adding an extension method is a great tip.
Awesome, glad you liked it :)
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()
There's no need to do AsQueryable if you get the Queryable directly from the DbContext
I always learn new things with your videos 😄
Always a new perspective on things that are familiar to me.
It's great! 😅
That's awesome to hear! Thank you :)
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)
I'll have to research about LinqKit 🤔🤔
@@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 :)
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.
Glad it was helpful!
Thank you and again greetings from Bulgaria.
As per your instruction and per my liking I smashed the like button.
Awesome, thank you! 💪😁
Love from Pakistan ! love that extension part.
Thanks a lot, Hassan! :)
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
Oh I'm planning to 😁 In fact, more than one video. And I'll even be open sourcing an existing implementation of mine.
@@MilanJovanovicTech awesome, look forward to it 😁
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.
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.
Great approach. such simple tweak but adds so much clarity to code
I only recently found out about this, but it's really helpful
i used it in various queries, thanks for the video 👏
Nice! I knew I can't be the only one 😁
You are a star mate!! Thanks for your tutorials!!
Happy to help
Very useful, adding it on my project 😛
Great!
Hello Milan, great video again!!
Any good advice or video for efcore bulk insert?
Not really, I would consider SqlBulkCopy
Very nice bit of technique.
Thank you, sir!
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.
In SQL 😁😁
@@MilanJovanovicTech the response is too short :)
Would be nice to see like an example.
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.
It's still a filter in the DB
another brilliant tutorial !!!
Thanks!
Hi, there is a way to remove the check empty in the generate sql query?
Nope, that's how EF Core translates it to SQL
Great content !!!
One question, where would you implement this extension method in a clean architecture? Is it in infrastructure layer?
Exactly, cause that's a reasonable place to do that. Ef core itself goes in there.☺️
I guess so, yeah. Or Persistence - if you have a separate layer for that.
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
Not that I know of, honestly 🤔
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
Thanks Milan😊
You're very welcome :)
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?
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
Good one Milan, thanks!
Glad you liked it!
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 :)
Won't have much effect on performance
This is great. I just added this WhereIf extension method to a new API I started working on. Thanks!
Awesome, glad you found it practical 😁
Amazing channel
Thank you very much!
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?
Wherever you'd need them to write LINQ queries
@@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?
You're amazing
Thanks!
Thanks Milan, you always amazing me 👍.
Happy to be of help 😁
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
I assume it's not added because you can achieve the same using just Where
Just a random suport comment .... great staff keep em coming ... ajde :)
You're the best!
How did you managed to print actual query in the console?
There's a method to configure EF output
@@MilanJovanovicTech
Can share code for doing that?
U are awesome man
Thanks a lot! :)
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
Generic in what way?
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
I find it very helpful
Glad it was helpful!
very nice😄
Thanks! Got any EF extension method of your own?
Does OData take care of empty filter internally?
I have no idea
Interesting 🤔
Indeed 🤔
Nice
Thanks!
project git link please
www.patreon.com/milanjovanovic
🌕