0

I have such code:

string[] list_lines = System.IO.File.ReadAllLines(@"F:\VS\WriteLines.xls");

System.Console.WriteLine("Contents of Your Database = ");

foreach (var line in list_lines.OrderBy(line => line.Split(';')[3]))
        {

            Console.WriteLine("\t" + line);
        }

I would like to TryParse the list_lines so they are numbers, not strings. Is it possible to 'bulk' it somehow? Each line consists of 5 strings after they are Split.


EDIT

I wrote this:

string[] list_lines = System.IO.File.ReadAllLines(@"F:\VS\WriteLines.xls");


        int[] newList;
        // Display the file contents by using a foreach loop.
        System.Console.WriteLine("Contents of Your Database = ");
        int.TryParse(list_lines[], out newList);

        foreach (var line in newList.OrderBy(line => line.Split(';')[3]))
        {

            // Use a tab to indent each line of the file.
            Console.WriteLine("\t" + line);
        }

But I get error on list_lines[] , it says that there must be a value.

4
  • Are you really trying to get int[] or you actually just want to order by the result of line.Split(';')[3] as int instead of string? Commented Apr 17, 2016 at 12:02
  • Yes, I want to order result by int not string. Commented Apr 17, 2016 at 12:04
  • So it seems that people are getting the question wrong (or maybe I am). Check my answer below, and see if it is what you meant to do. Commented Apr 17, 2016 at 12:09
  • 1
    I thought that in order to sort I need to get int. I didn't realize there is easier way Commented Apr 17, 2016 at 12:14

3 Answers 3

0

Based on your previous question, it seems that you want to order the lines by the 3rd split result as int, then you can do this way :

foreach (var line in list_lines.OrderBy(line => 
                                {
                                    int lineNo;
                                    var success = int.TryParse(line.Split(';')[3], out lineNo);
                                    if(success) return lineNo;
                                    return int.MaxValue;
                                }))
{
    Console.WriteLine("\t" + line);
}

I'm using int.MaxValue as default for when TryParse fails. This way, failed lines will come last. You can change the default to int.MinValue instead, if you want the failed lines to come first.

By the way, C# naming convention uses camel-case for variables, like lineNo and listLines instead of line_no and list_lines.


To get int[] that corresponds to each line, you can use similar logic, but now in a Select() method instead of OrderBy() :

int[] newList = list_lines.Select(line => 
                            {
                                int lineNo;
                                var success = int.TryParse(line.Split(';')[3], out lineNo);
                                if(success) return lineNo;
                                return int.MaxValue; //or whatever default value appropriate
                            })
                           .ToArray();
Sign up to request clarification or add additional context in comments.

3 Comments

This worked for me. Thanks a lot! I will study the code now to get into understanding what was done here. And yes, I forgot about the c# convention here. I will rename vars with _ .
I guess for now this is what I need. But in the future I would like also be able to make calculations on these string-to-integer values, and not only ordering and this is where int comes handy and necessary
@paddy See the 2nd code snippet above for a way to get the array of int
0

You can use SelectMany to flatten the list.

list_lines.SelectMany(line => line.Split(';')).Select(cell => int.Parse(cell));

If there can be non-number cells and you are looking for positive numbers you can add a Where clause

list_lines.SelectMany(line => line.Split(';')).Where(cell => cell.All(@char => char.IsDigit(@char))).Select(cell => int.Parse(cell));

Comments

0

One way of doing it:

int number;
var intList = list_lines.Select(s => s.Split(';')
                                      .Where(p => Int32.TryParse(p, out number))
                                      .Select(y => Int32.Parse(y)))
                                      .SelectMany(d=>d).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.