0

Is there a way i can pass a list of strings in the SqlParameter, lets say i have 10 authors and i want to find books published by them. I know i can make 10 parameters in (new SqlParameter) separately. But is there a way to just pass a list and get the results.

IQueryable<Book> queryable = client.CreateDocumentQuery<Book>(collectionSelfLink,
            new SqlQuerySpec
            {
                QueryText = "SELECT * FROM books b WHERE (b.Author.Name = @name)", 
                Parameters = new SqlParameterCollection() 
                { 
                      new SqlParameter("@name", "Herman Melville")
                }
            });   

1 Answers1

0

I think what you are looking for is the SQL IN keyword, see this link for more information.

Usage example:

SELECT *
FROM books
WHERE books.Author.Name IN ('Helena Petrovna Blavatsky', 
'Hermes Trismegistus', 'Heinrich Cornelius Agrippa')
  • Can this thing " 'Helena Petrovna Blavatsky', 'Hermes Trismegistus', 'Heinrich Cornelius Agrippa' " be passed as a parameter in Parameters, because i am trying to avoid the sql injection i want to pass a list of strings in Parameters, Can it happen ? – himanshu kaushal Mar 28 '16 at 05:42
  • @himanshukaushal The SQL `IN` Keyword is not supported yet in the DocumentDB Linq Provider AFAIK, see http://stackoverflow.com/questions/30870123/documentdb-in-keyword-with-linq . A workaround until they support the `IN` Keywords woudl be to execure the SQL query as above, and you can pass in a list of comma-seperated array elements using the `String.Join(",", authorsArray)` method. –  Mar 28 '16 at 11:13