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());

Different111222
  • 1,523
  • 3
  • 20
  • 28
  • possible duplicate of [How to create a List from a comma separated string?](http://stackoverflow.com/questions/910119/how-to-create-a-listt-from-a-comma-separated-string) – dash Jun 18 '12 at 13:21
  • Already covered on StackOverflow; hence vote to close. – dash Jun 18 '12 at 13:22

6 Answers6

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();
Ribtoks
  • 6,634
  • 1
  • 25
  • 37
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);
dodexahedron
  • 4,584
  • 1
  • 25
  • 37
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(',');
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
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));
Tigran
  • 61,654
  • 8
  • 86
  • 123
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(); 

        }
Marcello Faga
  • 1,134
  • 8
  • 12
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.

Gaz Winter
  • 2,924
  • 2
  • 25
  • 47
  • I should learn the basics first. I'm looking for something like List myList= myCSVstring.Split(',').ToList(); but this ivokes: Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List' and I do not find where to convert the string into an int. – Different111222 Jun 18 '12 at 13:49
  • 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) – Gaz Winter Jun 18 '12 at 13:51
  • 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? – Different111222 Jun 18 '12 at 13:54
  • This article might help with that bit http://stackoverflow.com/questions/44942/cast-listint-to-liststring-in-net-2-0 – Gaz Winter Jun 18 '12 at 14:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12699/discussion-between-gaz-winter-and-different111222) – Gaz Winter Jun 18 '12 at 14:15