-2
string[] questions= new[] { "2","5","1","11","3","1"};

    select description,count(description) 
      from descriptions 
inner join questions 
        on descriptions.qid=questions.id 
     where descriptions.question=N'hardtest' 
       and qid in (?) 
  group by description

I want to use an array in this code as an input to IN clause. How can I do that?

qid in ('" + list + "')
8
  • What is your qid type? Commented Jan 8, 2018 at 6:54
  • thank you bro , this is my first question Commented Jan 8, 2018 at 6:57
  • qid is string type Commented Jan 8, 2018 at 6:57
  • Possible duplicate of Passing a SQL parameter to an IN() clause using typed datasets in .NET Commented Jan 8, 2018 at 7:01
  • but qid can be an int type Commented Jan 8, 2018 at 7:04

1 Answer 1

2
string[] questions = new[] { "2", "5", "1", "11", "3", "1" };

string qids = string.Empty;
questions.All((x) => { qids += "','" + x; return true; });
qids = qids.Substring(2);

string sql = @"
select description, count(description)
  from descriptions
inner join questions
    on descriptions.qid = questions.id
where descriptions.question = N'hardtest'
   and qid in ("+qids+@"')
group by description";
Sign up to request clarification or add additional context in comments.

3 Comments

i'll wait until another optimized code ,if...
This is vulnerable to SQL Injection attack.
since the qid is int,the simpliest way is :questions.All((x) => { qids += Int32.TryParse(x,out id)?"','" + x:""; return true; });

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.