Efcore Dynamic Linq, AND and OR
Hi,
a textbook dynamic filter for optional properties in a dto would look like something like this
above would result in an SQL query like SELECT * FROM table WHERE condition1 AND ... Condition N.
I'd like to dynamically do SELECT * FROM table WHERE condition1 OR ... Condition N.
looking online, here and there, mostly people refer to using Expressions and delegates. which is fine if that's the most sane way about (I used delegates few times btw Func, Action...), Extensions? just read about them. tldr of my understanding? it allows me to compile and invoke code at runtime (cool*2). here <=(short url: og link has NO SSL!) after explaining some of the concepts/solutions, it kinda recommends using LinqKit(never touched it before).
Q1: are the discussed solutions optimal way around this?
Q2: is linqkit bread and butter? should I just used it instead of writing my own predicate builder with Or and other abilities? (best to think twice before adding a packages, especially if I'm gonna use it in one method at least for now) .your help is much appreciated! 🙏
37 Replies
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
I share my full method here
https://hastebin.com/share/onoxotapot.typescript
this very line in my post
is actually the simplified of the line below for sake of shortening the post itself:
but my main question is mostly about implementing a dynamic OR kinda query.
Hastebin
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
query.Where(x => x is y or z), no?
not understanding your problemthis is my DTO which has optional properties for searching
this query works but results in a sql query like I want to dynamically do
how can I do something like that?
just like that
keep chaining the
wheres on, if you want to keep narrowing
what is dto here?so you ingest an instance of this into the method above?
yes you're right and when narrowing search that works, but I wanna expand the search not WHERE but OR
the link I posted in the original post talks about something like this
the post is long im just pasting a sinppet here
then just... don't narrow it?
that code ^ seems crazy
fair enough how do I chain ORs with linq ?
dunamically
query.Where(x => cond1(x) || cond2(x))all my dto properties are optional
basically, "accept either of these conditions passing".
I don't see what the dto has to do with it... you project into the dto from the queried rows, no?
public record SearchBlogDTO
{
public List<string>? Tags { get; set; }=null;
public List<string>? Domain { get; set; }=null;
public string? Author { get; set; }=null;
public DateTime? CreatedBefore { get; set; }=null;
public DateTime? CreatedAfter { get; set; }=null;
public string? IncludesTitle { get; set; }=null;
public string? IncludesTitleBody { get; set; } = null;
}
the controller can receive
dto{Tags[]}
dto{Tags[], Author:""}
and so on
so if I donno beforehand which of the properties are or arent there me writing
query.Where(x => cond1(x) || cond2(x)) becomes tricky
this is the part I can't wrap my head arounduse those two examples and write the full method to the best of your ability
comment the part you don't know
bc i simply do not understand your issue
fair enough, I appreciate you entertaining the idea anyway 🙏
for instance
you can also just inline it
you want to use a different
where depending on the Author, for instance?
just make different methodsyes that would be one way about it
thanks a lot!
i think you're overcomplicating it
i admit i am, but in a different project where i was doing raw sql, this was simpler to achieve and its bothering me that when it comes to OR i haven't been able to find the eqivalent with efcore, and the closest I have come is the use of Extension tree,
...? but that's not true
.Where(x => x is y or z)
this accomplishes an or
back to your example, if we have an Author, how does the clause need to change?
also, you don't need to do = null for all of those, it's impliedyou're right, but what if you donno if there is gonna be a third fourth and nth condition ?
you check them?
idgi
x is y or z or foo or baz or bar or...I will look into the solution you provided and will report back if it works
you can do any arbitrary conditions you want, if EF cannot compile them it will tell you
i admit I know very little, so forgive me if I don't understand something here
query = query.Where(i => i.name == dto.name || i.age == dto.age);
what happens if dto.age is null ?
then it will match records with null age
the behaviour I want is not to include age condition if its null
if you want to exclude that,
i => i.name == dto.name || (dto.age != null && i.age == dto.age)
it's just a chain of boolean expressionsok let me try! 🙂 if this works I will postpone learning Expression tree
it works :kekw:
I appreciate it regardless! truly! thank you for entertaining my dumb question!
have you done much work with lists and filtering outside of EF?
if you're primarily a SQL dev, you'll want to pick up some general linq
i have gotten by, by using where, disticnt, groupby and select so far
I have primarily used ado.net (horrible experience) at work
and currently learning efcore
rebuilding few projects done in ado.net ....
sure, but do you also just write data manipulation in c# (outside of queries)?
because linq is fundamental (or should be, anyway)
ie, learn linq first. a lot of efcore is just linq.
thank YOU!
sorry for the headache I gave you, and thanks for you patience!
no worries