简体   繁体   中英

Get first middle and last 10 line on textbox

I have text on textbox and I want to get the first 10 middle 10 and last 10 line on the text box.

Example below uses 2 in order to not give long sample data

sample data

1.2
1.44
1.68
1.44
1.44
1.2
1.68
1.68
1.68
1.68

expected output

1.2
1.44
1.44
1.2
1.68
1.68

My attemp.

var source = txtProcessData.Lines;
                    var first = source.Take(2);
                    var last = source.Skip(source.Length - 2);

                    txtProcessData.Text = String.Join(Environment.NewLine, first);

but has error:

Error 2 Argument 2: cannot convert from 'System.Collections.Generic.IEnumerable' to 'string[]'

Note Data lines will be always be even number because I have condition for that. I also use 2 on sample.

How can I achieve this?

You can try a simple Where :

var source = txtProcessData.Lines;

var result = source
  .Where((value, index) => index <= 1 ||                     // first  2 lines
                           index >= source.Count - 2 ||      // last   2 lines
                           index == source.Count / 2 ||      // middle 2 lines
                           index == source.Count / 2 - 1);

// ToArray() - Early versions C# want string[] for string.Join
txtProcessData.Text = String.Join(Environment.NewLine, result.ToArray());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM