Is there a way to parameterize Contains query?
See original GitHub issueCurrently linq query with Contains method generates sql operator “in” with inlined parameters.
For example:
[Test]
public void __Linq2Db_InlineParameters_For_Contains()
{
var ids = new int[] { 1, 2, 3 };
using (var db = GetConnection())
{
var query = db.Users.Where(x => ids.Contains(x.Id));
Console.WriteLine(query);
}
}
generates:
SELECT
[t1].[Id],
[t1].[Name],
[t1].[IsActive],
[t1].[AppointmentId]
FROM
[Users] [t1]
WHERE
[t1].[Id] IN (1, 2, 3)
Is there a way to generate something like [t1].[Id] IN (@p0, @p1, @p2)?
Environment details
linq2db version: 1.10.1 Database Server: SqlServer 2016 Database Provider: SqlServer.2012 Operating system: Windows 10 Framework version: .NET Framework 4.6.2
Issue Analytics
- State:
- Created 6 years ago
- Comments:46 (22 by maintainers)
Top Results From Across the Web
Use parameters to ask for input when running a query
Open the union query in SQL view. · Add a WHERE clause that contains the fields you want to add parameters to. ·...
Read more >sql server 2008 - Parameterize an SQL IN clause
How do I parameterize a query containing an IN clause with a variable number of arguments, like this one? SELECT * FROM Tags...
Read more >Using parameterized queries to avoid SQL injection
Parameterized queries is a technique that aims to separate the SQL query from the user input values. The user input values are passed...
Read more >Passing parameters in CONTAINS using wildcard
I am having a difficult time trying to find a way to use an SQL string in ASP.NET to pass a parameter to...
Read more >Using parameterized queries - Amazon Athena
You can use Athena parameterized queries to re-run the same query with different parameter values at execution time and help prevent SQL injection...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@sdanyliv you know, I think I managed to autoreplace Contains to something parameterized… To list of OR-s. Without extensions, cache disabling and stuff
If you have time, can you share your thoughts about this solution?
Test:
output:
You are right. Your transformation is not ideal and do not not handle situations with SelectMany when used Method that returns IQueryable and has
Contains
inside. You have to Invoke this method and grab Expression returned by this method.I will continue to work with correct cache hit. Looks like we have problem here.