1

I'm using C# ASP.NET 4 and SQL Server 2008 R2

I'm getting an object scalar from sql server which is a string containing comma separated values of the form:

7, 12, ... 1, 65

I would like to convert this object into a list?

I thought of the direction:

List<int> myList = new List<int>(new int[] (List)mySqlClass.mySqlQueryReturningAnObject("SELECT CLAUSE");

but this is not going to work.

How do I convert this object into a list?


Full answer:

This answer in use is according to the selected answer (before the update)

List<int> myList = new List<int>(mySqlClass.mySqlQueryReturningAnObject("SELECT CLAUSE").ToString().Split(',').Select(x => Convert.ToInt32(x)).ToList());

2

6 Answers 6

17
var intValues = line.Split(',').Select(x => Convert.ToInt32(x)).ToList();

Update

To ensure your code would be able to process strings like 1,2,3,,,4,5,6 you can use overload of String.Split method

var intValues = line.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries)
                    .Select(x => Convert.ToInt32(x))
                    .ToList();
Sign up to request clarification or add additional context in comments.

Comments

5

Use the .Split() method on the string. It will return an array of strings.

string yourResult = "1,2,3,4,5";
string[] resultsArray = yourResult.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);

Comments

2

Did you try String.Split?

You can split your string with a line as simple as this:

var myString="1,2,4,5,6";
var numberList=myString.Split(',');

Comments

2

You can use simple Array.ConvertAll instuction, like this:

string str = "1,2,3,4,5,7,8,9";
int[]resultInArray = Array.ConvertAll( str.Split(','), item => int.Parse(item));

Comments

2
        private void TestParse()
        {
            string commaseparatedstring = "3, 5,6,19";

            int parsedvalue = 0;

            List<int> valuesint =
                commaseparatedstring.Split(',').Select(elem => int.TryParse(elem, out parsedvalue) ? parsedvalue : 0).ToList(); 

        }

Comments

0

You could try assigning the result to a variable.

Then convert it into a string (if its not already one)

Then do a string split on the comma and assigning the results to a list.

5 Comments

I should learn the basics first. I'm looking for something like List<int> myList= myCSVstring.Split(',').ToList(); but this ivokes: Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<int>' and I do not find where to convert the string into an int.
Do you need them as an int? If not then just make your list a list of strings. In fact you could still do that and then when you read them out of the list you can do a Convert.ToInt32(listitem)
Yes I need them as ints. Yes, I did that, I read the string and convert them to ints upon read. Is there a syntax for doing it upon assitnment?
This article might help with that bit stackoverflow.com/questions/44942/…

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.