Home > Software design >  C# parse List<List<List<String>>> TO List<List<List<Int>>>
C# parse List<List<List<String>>> TO List<List<List<Int>>>

Time:08-24

can somebody please help me about this problem.

private int[] strategija1 (DataTable dtexcel)
        {   
            List<List<List<String>>> dt_matrix = convert_list_3d_arrayList(dtexcel, dtexcel.Rows.Count, (dtexcel.Columns.Count) - 1); 

            make_me_a_travel_matrix(dt_matrix);
            return null;
        }

 public Array make_me_a_travel_matrix(List<List<List<int>>> list_matrica)
     {
        int[,,,] Put = new int[20, 20, 20, 20];
        int i = 1;
        int pol = i;
        int j = 0;
        int Poc1=0, Poc2=0, Poc3=0;
        int x = 1, y = 1, k = 1;
        int odr = 0;
        korakB(ref  i, ref Put, ref  pol, ref j, ref x, ref y, ref k, ref odr, ref list_matrica, ref Poc1, ref Poc2, ref Poc3);
        for (int a = 0; a < Put.GetLength(0); a  )
        {
            for (int b = 0; b < Put.GetLength(1); b  )
            {
                Console.WriteLine(Put[pol, odr, a, b]);
            }
        }
        return Put;
     }

I am receiving error on "dt_matrix" inside "make_me_a_travel_matrix(dt_matrix);"

> Severity  Code    Description Project File    Line    Suppression State
> Error CS1503  Argument 1: cannot convert from
> 'System.Collections.Generic.List<System.Collections.Generic.List<System.Collections.Generic.List<string>>>'
> to
> 'System.Collections.Generic.List<System.Collections.Generic.List<System.Collections.Generic.List<int>>>'  WindowsFormsApp1    ...\WinFormsApp1\WindowsFormsApp1\Form1.cs  262 Active

Is there way to parse List<List<List<String>>> to List<List<List<Int>>>?

CodePudding user response:

Here is what you want I think:

var newList = dt_matrix.Select(l => l.Select(l2 => l2.Select(int.Parse)));

This code will only work if you're sure every string you have can indeed be parsed into an int. This code will throw an exception if it encounter a string that cannot be parsed.

CodePudding user response:

When you have a problem like this it is sometimes much easier when you break it down into subproblems.

First write a method to convert a single element:

public static int ConvertElement(string input) => int.Parse(input);

Then write a method to convert a list, using the first method to convert each element:

public static List<int> ConvertList(List<string> input) => input.Select(ConvertElement).ToList();

Then write a method to convert a list of lists, using the second method to convert each list.

public static List<List<int>> ConvertListList(List<List<string>> input) => input.Select(ConvertList).ToList();

Now your solution is easy:

public static void Main()
{
    var input = new List<List<string>>();
    List<List<int>> output = ConvertListList(input);
}

But if you prefer you can do it one method:

public static List<List<int>> ConvertListOfLists(List<List<string>> input)
{
    return input.Select
    (
        x => x.Select(int.Parse).ToList() 
    )
   .ToList();
}

CodePudding user response:

use a combination of Linq and Int.Parse like:

var string_list = new List<string>(){ "1","2","3" };
var int_list = string_list.Select( x => Int32.Parse( x ) );

CodePudding user response:

You can either use LINQ or a simple set of for loops. When it comes to parsing, there are several options. One is to use the int.Parse method which will convert the string to a number if it can. If it cannot, it will throw an exception. If you want to check whether a number can be parsed, you can use int.TryParse method as an alternative.

Examples

Using LINQ

List<List<List<int>>> Parse(List<List<List<string?>>> input)
{
    return input.Select(a => a.Select(b => b.Select(int.Parse).ToList()).ToList()).ToList();
}

Using a set of for loops

List<List<List<int>>> Parse(List<List<List<string?>>> input)
{
    var numbers = new List<List<List<int>>>();

    for (int i = 0; i < input.Count; i  )
    {
        numbers.Add(new List<List<int>>());
        for (int j = 0; j < input[i].Count; j  )
        {
            numbers[i].Add(new List<int>());
            for (int z = 0; z < input[i][j].Count; z  )
            {
                var newValue = int.Parse(input[i][j][z]);
                numbers[i][j].Add(newValue);
            }
        }
    }

    return numbers;
}

Usage

var result = Parse(yourStringCollection);
  •  Tags:  
  • c#
  • Related