0

I have one text box the value is like that below

1
2
3
1
3

I tried:

textBox1.Text = string.Join("\r\n", **textBox1.Lines**.Distinct());

textBox1.Lines is not supporting to my program

Carrie Kendall
  • 11,124
  • 5
  • 61
  • 81
Sijo K.S
  • 21
  • 4

4 Answers4

1

Add this to using directives at top of page:

using System.Linq;

Then simply use like so:

textBox1.Text = string.Join(Environment.NewLine, textBox1.Lines.Distinct());
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
1

What do you think about that:

        string x = "1\r\n2\r\n1\r\n";

        string[] lines = x.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        var cmd = (from i in lines select i);
        string[] result = cmd.Distinct().ToArray();

        x = string.Join("\r\n", result);
walruz
  • 1,135
  • 1
  • 13
  • 33
1
string[] distinctLines = theText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).Distinct().ToArray();
textBox1.Text = string.Join("\r\n", distinctLines);
Graffito
  • 1,658
  • 1
  • 11
  • 10
0

Even though the code you posted should work, here's a different approach.

textBox1.Text = string.Join(Environment.NewLine, textBox1.Text.Split(new[] {Environment.NewLine}, StringSplitOptions.None).Distinct());

Basically, you can split the text into lines yourself since you mention problems with Lines.

imlokesh
  • 2,506
  • 2
  • 22
  • 26