C#C
C#•2y ago
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 })
//...
}

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

code side, I have a list looking like
| 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
}

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
EF Core Where on multiple fields
Was this page helpful?