0

I need use a query like

SELECT * FROM Table WHERE field IN (1,2,3)

I c# use this

var data = context.ExecuteQuery<Some>( "SELECT * FROM Table WHERE field IN {0}", arrayParam ); //for example arrayParam has 1,2,3 

When I used this, works fine

 var data = context.ExecuteQuery<Some>( "SELECT * FROM Table WHERE field = {0}", 1);

But in with param crash

as passing the array of parameters?

NOTE: The real query is It is much more complex, for this reason use native query instead linq

2
  • There's no such thing unfortunately, you will have to pass each parameter separately using named parameters, or use string concatenation Commented Jun 27, 2015 at 18:55
  • "Where(field = 1 or field = 2 or field = 3 or field = 4)" You can easily generate the where clause string in C#. Lot of way of doing this. Can help. Commented Jun 27, 2015 at 19:21

2 Answers 2

4

If you verify your array before passing it as a SQL param (to prevent SQL injection), you can do something like this:

var data = context.ExecuteQuery<Some>( "SELECT * FROM Table WHERE field IN ({0})", string.Join(",",arrayParam.Select(n=>n.ToString())));
Sign up to request clarification or add additional context in comments.

Comments

0

You can do what @Marcin Zablocki says, or you can construct a string with your entire query and call ExecuteQuery without any param like this:

var query = string.Format("SELECT * FROM Table WHERE field IN ({0})", string.Join(",", arrayParam.Select(n => n.ToString())));

var data = context.ExecuteQuery<Some>(query);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.