Converting List<string> to List<double>

Can't get anything working :(

Page 1 of 1

4 Replies - 4645 Views - Last Post: 18 March 2010 - 10:38 AM Rate Topic: -----

#1 Zdrenka  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 116
  • Joined: 24-November 09

Converting List<string> to List<double>

Posted 18 March 2010 - 09:33 AM

Hi again,
your all most likely sick of me now, but I'm having another problem and would really appreciate your help.

           
 List<double[]> HAL = heartlist.Select(x => (double[])x).ToList();
            //heartlist = HAL;

           foreach (double[] value in HAL) // Loop through List with foreach
           {
               //richTextBox2[].Text = value.ToString();
               PointPairList list = new PointPairList();
               list.Add(null, value);


I'm using this code to Get information from a List<string> to Zedgraph, in order to do this I must first convert the List<string> to a List<double>

to do this I have used this method. (heartlist is the List<string>)
List<double[]> HAL = heartlist.Select(x => (double[])x).ToList();


Although I'm getting this error:
Cannot convert type 'string' to 'double[]'

Any help?

Is This A Good Question/Topic? 0
  • +

Replies To: Converting List<string> to List<double>

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Converting List<string> to List<double>

Posted 18 March 2010 - 10:13 AM

Here's an option (This code is untested but hopefully it will get you down the right path). This code takes a delimited string, in your case I would format it something like this myList = "2,3 | 1,4 | 9,6" for example, then have it split on the delimiter |

public List<double[]> ConvertList(string inList, string delimiter)
{
    List<double[]> returnList = new List<double[]>();
    returnList.AddRange((
                   from item in inList.Split(delimiter.ToCharArray())
                   select ((double[])Convert.ChangeType(item, typeof(double[])))
               ).ToList());
    return returnList;

}



I may be way off on this one so take this with a grain of salt until I can get more research into this. I've never tried converting a List<string> to List<double[]> before so bear with me lol
Was This Post Helpful? 0
  • +
  • -

#3 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Converting List<string> to List<double>

Posted 18 March 2010 - 10:15 AM

I'd don't advice to use code you don't fully understand, like lambda expressions at the moment. If you have no idea what x => (double[])x does, you should find another way to accomplish what you need. For example you could loop through the List<string> and convert each item into double.
List<double> doubles = new List<double>();
for (int i = 0; i < heartlist.Count; i++) {
  doubles.Add(double.Parse(heartlist[i]));
}



edit: some fail code, whoops.

This post has been edited by janne_panne: 18 March 2010 - 10:16 AM

Was This Post Helpful? 1
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Converting List<string> to List<double>

Posted 18 March 2010 - 10:20 AM

In the OP's defense they're just using the code I offered in another thread, which would have worked fine for converting a List<string> to List<double>, but not for List<string> to List<double[]>
Was This Post Helpful? 0
  • +
  • -

#5 Zdrenka  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 116
  • Joined: 24-November 09

Re: Converting List<string> to List<double>

Posted 18 March 2010 - 10:38 AM

Thanks guys, again, this is a really big help!

PsychoCoder I couldn't get it working this way either
List<double> HAL = heartlist.Select(x => (double)x).ToList(); 


It gave me the same error, maybe I'm doing something wrong though. janne_panne I think your right about not using code you don't understand, although I am now trying to figure out how it works lol.

Thanks again guys! I Think I'm ok now. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1