EF Core Where on multiple fields
Initial question
Hi!
I'm currently running into an issue where I try and do something along the lines of
The whole idea behind that is to check {f.bar, f.buzz} against a list of {argBar, argBuzz} with something like
Where(f => args.Contains(new { bar = f.bar, buzz = f.buzz }))
I can't find anything online and I'm quite stuck... (But I'm pretty sure I'm phrasing my google query wrong)
A workaround our senior dev provided was to concatenate bar and fuzz into a string
Although that works, the query time went absurdly high...
If anyone could lead me into the correct direction, that would be highly appreciated š
More context with example and better exaplanation
I've got a table with 2 cols Foo and Bar
My database has those records
code side, I have a list looking like
I'd like to fetch every record from my database where a corresponding record is found in my list
In this example, only
would be fetched
And .Where(x => x.Foo == argFoo && x.Bar == argBar)
would work just fine if I only one pair of argument to match the records from
But here, I'd like something more akin to a list.Contains(dbRecord)
Answer
With LinQKit
https://discord.com/channels/143867839282020352/1231977108239290418/123198580920968818818 Replies
? š
or in a single
A workaround our senior dev provided was to concatenate bar and fuzz into a string Although that works, the query time went absurdly high...š¬ dont do that.. say goodbye to indexes and db-side filtering
That works just fine!
Until I try and match against records from a list where my db record should be equal to at least a record from my list
That's where I'm hitting a wall
yeah, although it works, that really sounds like a bad option, ngl
i dont understand what you're doing
can you show example?
I'll give a bit more context
I've got a table with 2 cols
Foo
and Bar
My database has those records
code side, I have a list looking like
I'd like to fetch every record from my database where a corresponding record is found in my list
In this example, only
would be fetched
And .Where(x => x.Foo == argFoo && x.Bar == argBar)
would work just fine if I only one pair of argument to match the records from
But here, I'd like something more akin to a list.Contains(dbRecord)is this code side list expected to be small like few entries or?
thousands of records in the database
About 1 to 50ish list items depending on user input (most case should be 1 to 3 list item)
those two fields are part of the primary key which is composed of 4 fields total
I may just generate a raw sql query with a foreach on the list, lmao
for something like
doesn't sound a lot better than the string concatenation our colleague told us to do š¤
(well, it will at least keep the indexes. But that doesn't look like the cleanest nor safest)
could just do equivalent of that in ef core then.
i don't know the best approach tbh
i don't know the best approach tbheh, you showed up. That alone helped me a lot, thanks š
considering its a dynamic list for filtering, you'd need to build expressions or use something like linqkit i think
linqkit ? I'll look it up
PredicateBuilder and chain .Or expresions
like this example
but again idk if this is best solution tbh
oh, wow. Didn't know about that. That's cool!
Well, I'll at least check it out š
That sounds like a good idea
I'll let the thread open, check out the solution, post my answer and close it if someone ever has the same kind of problem
š
Again, thanks a lot
np!
Works like a charm š
nice
tera
like this example
Quoted by
<@235149642445225984> from #EF Core Where on multiple fields (click here)
React with ā to remove this embed.