Ā© 2026 Hedgehog Software, LLC

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

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
void SomeMethod(string argBar, int argBuzz){
//...
efCoreContext.FooTable.Where(f => new { bar = f.bar, buzz = f.buzz } == new { bar = argBar, buzz = argBuzz })
//...
}
void SomeMethod(string argBar, int argBuzz){
//...
efCoreContext.FooTable.Where(f => new { bar = f.bar, buzz = f.buzz } == new { bar = argBar, buzz = argBuzz })
//...
}

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 }))
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
| Foo | Bar |
| 1   | 1   |
| 1   | 2   |
| 1   | 3   |
| Foo | Bar |
| 1   | 1   |
| 1   | 2   |
| 1   | 3   |

code side, I have a list looking like
| Foo | Bar |
| 1   | 1   |
| 2   | 1   |
| 3   | 1   |
| Foo | Bar |
| 1   | 1   |
| 2   | 1   |
| 3   | 1   |

I'd like to fetch every record from my database where a corresponding record is found in my list
In this example, only
{
 Foo : 1,
 Bar : 1
}
{
 Foo : 1,
 Bar : 1
}

would be fetched

And
.Where(x => x.Foo == argFoo && x.Bar == argBar)
.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)
list.Contains(dbRecord)


Answer

With LinQKit
EF Core Where on multiple fields
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

ā” Ef core multiple projects
C#CC# / help
3y ago
EF Core - Where clause on multiple field in object list
C#CC# / help
2y ago
ā” EF Core optional where extension method
C#CC# / help
3y ago
EF Core
C#CC# / help
2y ago