I have a list of field combinations that I want to search for. The list can realistically contain up to 100 items, the table I'm querying has a little over 1 million records at this time.
An example:
create table mytable
(
foo int not null
,bar int not null
,baz int not null
)
insert into
mytable
values
(1, 1, 11)
,(1, 2, 12)
,(1, 3, 13)
,(2, 1, 21)
,(2, 2, 22)
,(2, 3, 23)
,(3, 1, 31)
,(3, 2, 32)
,(3, 3, 33)
One possible way to retrieve data:
select
foo
,bar
,baz
from
mytable
where
(foo = 1 and bar = 3)
or (foo = 2 and bar = 1)
or (foo = 3 and bar = 2)
Another possible way:
declare @filtercombos table
(
foo int not null
,bar int not null
)
insert into
@filtercombos
values
(1, 3)
,(2, 1)
,(3, 2)
select
mytable.foo
,mytable.bar
,mytable.baz
from
@filtercombos fc
left join mytable on mytable.foo = fc.foo and mytable.bar = fc.bar
Both will return this data:
foo bar baz
----------- ----------- -----------
1 3 13
2 1 21
3 2 32
Now, if this was a list of single values, I could do .Where(item => myList.Contains(item.foo))
. How can I do a query like above? The only thing I can think of is to execute SQL on the DbContext, but I'd like to avoid that if possible.