© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
22 replies
Alta

EF Core - Where clause on multiple field in object list

Hi everybody
I'm using sql through EF Core, here at work, and I have a problem querying multiple results from a table
In SQL, I've got that table of
Foo
Foo
with an index on field
a
a
and
b
b

In my code, I've got a list of
Bar
Bar
containing, quite conveniently, two field
a
a
and
b
b

I'd like to do something along the line of
sqlContext.Foo.Where(f => f.a == bar.a && f.b == bar.b);
sqlContext.Foo.Where(f => f.a == bar.a && f.b == bar.b);

But where foo could match with any element of an enumerable of bar

My colleague suggest something looking like this
IEnumerable<string> stringifiedBar = bars.Select(bar => $"{bar.a}{bar.b}");
sqlContext.Foo.Where(foo => stringifiedBar.Contains(foo.a+foo.b));
IEnumerable<string> stringifiedBar = bars.Select(bar => $"{bar.a}{bar.b}");
sqlContext.Foo.Where(foo => stringifiedBar.Contains(foo.a+foo.b));

Super, that works
But...
It's slow as heck
Our guess is that it try out every row in the table foo?
Like, he can't really know the result of foo.a + foo.b so it does it for every single row before checking it for a match inside of stringifiedBar?

How would one do that query properly?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

EF Core Where on multiple fields
C#CC# / help
2y ago
❔ Ef core multiple projects
C#CC# / help
3y ago
❔ EF Core Person Task List
C#CC# / help
3y ago
✅ Designing a configurable object in EF Core
C#CC# / help
17mo ago