0

I have a list of strings called parentRows which contains strings that look like this:

"176,c-4454"
"177,c-34324"

I've used .split(',') to put them in a string[]

var arr = parentRow.Split(',');

because essentially what I want is that when I loop through the list, if the user input matches arr[1], then I want to convert arr[0] to an int and store it in the user's information.

Code snippet:

if (p.ParentCode != "")
{                   
    foreach(var parentRow in parentRows)
    {
        var arr = parentRow.Split(',');
                           
        if(arr[1] == p.ParentCode)
        {
            int asInt = arr[0].Select(s => int.Parse(s));
        }
    }
}

as you can see I've tried int asInt = arr[0].Select(s => int.Parse(s)); but I just get the error

"Error CS1503 Argument 1: cannot convert from 'char' to 'System.ReadOnlySpan<char>"

How would one go about achieving what I want?

Thank you in advance!

2 Answers 2

5

I guess you just want to parse element 0 (arr[0])

int value =  int.Parse(arr[0])

The long story is SomeString.Select projects a string to an array of char, and int.Parse has an overload for ReadOnlySpan. The compiler assumes through the overload resolution that is the best fit for what want to do. Which is what the error is telling you

"Error CS1503 Argument 1: cannot convert from 'char' to 'System.ReadOnlySpan"

Sign up to request clarification or add additional context in comments.

Comments

2

Try below code:

if(! string.IsNullOrEmpty(p.ParentCode) )
{
    var l = parentRows
      .Select(r => r.Split(','))               // split every item in array by comma
      .Where(r => r[1] == p.ParentCode)        // select only those, which second element matches the code
      .Select(r => int.Parse(r[0])).ToArray(); // select only first element of slpitted string casting it to int
}

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.