1

I have string list

string [] lines = {"1","2","3"};

Then I have another string list

string [] linesTwo = {"2.1","2.2","2.3"};

How can I make a combination of those lists when checkBox1 is checked? If checkBox1 is checked I would like to have list like:

{"1","2","2.1","2.2","2.3","3"}

EDIT: My lists do not actually contain any numbers so any sorting is not possible.

kah
  • 35
  • 6
  • Can you check this url http://stackoverflow.com/questions/1547252/how-do-i-concatenate-two-arrays-in-c – vinodh May 10 '16 at 08:00

6 Answers6

1

I have written code for your problem. I am just sorting the array by using Sort attribute for giving your exact output.

string [] lines = {"1","2","3"};
    string [] linesTwo = {"2.1","2.2","2.3"};
    var CombinedString = new string[lines.Length + linesTwo.Length];

    Array.Sort<string>(CombinedString );



    if(CheckBox1.Checked)
        {
           lines.CopyTo(CombinedString , 0);
            linesTwo.CopyTo(CombinedString , lines.Length);
            Array.Sort<string>(CombinedString );
        }

Fiddle Code here..Check this way

vinodh
  • 714
  • 1
  • 7
  • 20
  • Is there any other way than sorting? If lists don't contain any numbers and I still want to linesTwo to start at 3rd row and also have that "3" at the end. I could to separate string "lines" to two different pieces but I don't feel that it is right. – kah May 10 '16 at 09:21
  • If i understand your requirement correctly, DO you need to add linesTwo string add inside the first string array after 2 elements... ? Can you please ellaborate your question..:) – vinodh May 10 '16 at 09:26
  • Yes. First I tried like this if it helps to understand. lines={"1","2",linesTwo,"3"} – kah May 10 '16 at 09:30
0

Concat the two and sort the items.

var newline = lines.Concat(linesTwo).OrderBy(i => i);
Marko Juvančič
  • 5,792
  • 1
  • 25
  • 41
0

You use this to merge two lists of the same type:

list = list1.Concat(list2).ToList();
diiN__________
  • 7,393
  • 6
  • 42
  • 69
Ankur Shah
  • 412
  • 6
  • 16
0

This largely depends on how you view is set up. Assuming you are using WPF, you could use a CompositeCollection:

<CompositeCollection>
     <ColectionContainer Source="{Binding listOne}" />
     <ColectionContainer Source="{Binding listOne}" />
</CompositeContainer>

If you are not using WPF, it is likely you will have to manually control this by subscribing to an event on the Checkbox:

public void Checkbox1_Checked(...) {
    combinedList = lines.Concat(linesTwo);
    ...
}

If you can provide a little more information on what you're trying to do, we might be able to help a little more: What code creates your checkbox? how are these lists going to be used?

Richard SP.
  • 497
  • 6
  • 15
0

If you asking how to do that on webforms than:
1) Set your textBox to autoPostBack
2) generate event CheckedChanged
3) Code below is code in that event

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)//checkbox name CheckBox_1
{
            string[] merged;
            string[] lines = { "1", "2", "3" };
            string[] linesTwo = { "2.1", "2.2", "2.3" };
            if(CheckBox1.Checked) // check is checkbox checked
            {
                merged = lines.Concat(linesTwo).ToArray(); // merge
            }

}
0

using Linq Concat you can concat the two arrays. Then you need to sort them using OrderBy,

var newline = lines.Concat(linesTwo).OrderBy(i => i).ToArray();
Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59