-6

I have this array:

enter image description here

And I need to make from array above string,one string like this:

(export_sdf_id=3746) OR (export_sdf_id=3806) OR (export_sdf_id=23) OR (export_sdf_id=6458) OR (export_sdf_id=3740) OR (export_sdf_id=3739) OR (export_sdf_id=3742)

Any idea what is the elegant way to implement it?

Michael
  • 13,950
  • 57
  • 145
  • 288
  • what defines whether or not a different array location is used? – Takarii Jan 28 '16 at 10:16
  • Did you try searching? Please read [ask]. – CodeCaster Jan 28 '16 at 10:17
  • Why do you need the brackets? – MakePeaceGreatAgain Jan 28 '16 at 10:18
  • 1
    Why is someone voting to reopen? This question has been asked thousands of times before, and this question does not follow the guidelines found in [ask]. There is no research shown. – CodeCaster Jan 28 '16 at 10:24
  • @CodeCaster both given answers are wrong – fubo Jan 28 '16 at 10:24
  • 1
    here is the correct answer `string result = String.Join(" OR ", idsArr.Select(x => '(' + x + ')'));` – fubo Jan 28 '16 at 10:29
  • @CodeCaster looks like you've picked a bad dupe target here; the target involves joining strings without any delimiter, whereas this one needs to wrap them in brackets *and* join them with `OR`s. That warrants a reopen vote, IMO, although there's probably another, better, dupe lying around (and the question certainly ain't *good*). – Mark Amery Jan 30 '16 at 12:08
  • 5
    @Mark how about [this one](http://stackoverflow.com/questions/35139228/concatenate-string-collection-into-one-string-with-separator-and-delimiters)? – CodeCaster Feb 01 '16 at 21:23
  • @CodeCaster looks better. :) – Mark Amery Feb 01 '16 at 22:17

2 Answers2

3

There is the String.Join-method designed for this.

var mystring = String.Join(" OR ", idsArr);

This will result in the following string:

export_sdf_id=3746 OR export_sdf_id=3806 OR export_sdf_id=23 OR export_sdf_id=6458 OR export_sdf_id=3740 OR export_sdf_id=3739 OR export_sdf_id=3742

Note that the brackets are omited as they are not needed for your query.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
2

You can use String.Join(String, String[]), where first parameter is separator between array elements.

string result = String.Join(" OR ", sArr);
Adil
  • 146,340
  • 25
  • 209
  • 204