_databaseContext.Names.Where(x => strings.Contains(x.name) && specialID == 1);
If your list of strings is unique, use can use HashSet<>
instead of List<>
. I think it will give you better performance.
HashSet<string> hs = new HashSet<string> { "test1", "test2", "etc" };
_databaseContext.Names.Where(x => hs.Contains(x.name) && specialID == 1);
EDIT: +1 to Alexei Levenkov for pointing out- Exists
will cause problems, should use Contains
instead. Also, verified the same query generated for List
as well as HashSet
so in case you want to query on server, is does not matter whether you use List
or HashSet
.
Generated query will be some thing like.
Select [name], [other columns]
From Names
Where [name] IN (N'test1', N'test2', N'etc')
However, assuming specialID
is local variable, it will generate a dynamic query similar to above but also pass its value to SQL Server. This query is executed using sp_executesql
.