1

I'm trying to convert the string separated by commas which has 7 values of:

2014-21-2,1207.81,1209.87,1202.84,1203.79,1862300,1203.79

To another model which is:

return lines[1].Split(',').Select(i => new StockModel
{
    StockDate = DateTime.ParseExact(i.ToString(), "yyyy-MM-dd", null),
    StockOpen = float.Parse(i.ToString()),
    StockHigh = float.Parse(i.ToString()),
    StockLow = float.Parse(i.ToString()),
    StockClose = float.Parse(i.ToString()),
    StockVolume = Convert.ToInt32(i.ToString()),
    StockAdjustedClose = float.Parse(i.ToString()),
    StockSymbol = stockSymbol

}).SingleOrDefault();

However I get errors such as: Additional information: Input string was not in a correct format. http://s17.postimg.org/ro4k3tzct/Screenshot_1.png

If I do it manually like: DateTime date = DateTime.Parse(lines[1].Split(',')[0]), it works fine.

Whatever value I'm trying to put into the new Model, I get errors such as this one.

2
  • 2/21/2014 doesn't match yyyy-MM-dd. Isn't it obvious for you? Commented Feb 24, 2014 at 0:17
  • You're missing a coma on one line, and you have an extra one on another line. Commented Feb 24, 2014 at 0:19

1 Answer 1

2

OK, I see the problem. You shouldn't use Select here. Try following instead:

var i = lines[1].Split(',');

return new StockModel()
{
    StockDate = DateTime.ParseExact(i[0].ToString(), "yyyy-MM-dd", null),
    StockOpen = float.Parse(i[1].ToString()),
    StockHigh = float.Parse(i[2].ToString()),
    StockLow = float.Parse(i[3].ToString()),
    StockClose = float.Parse(i[4].ToString()),
    StockVolume = Convert.ToInt32(i[5].ToString()),
    StockAdjustedClose = float.Parse(i[6].ToString()),
    StockSymbol = stockSymbol
};
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, this would probably work and I was going to end up doing this. The select makes so much sense to me but it seems it dosent work.
Select would work if you'd try to get one StockModel instance for each part of the string. What you really need is one StockModel for the entire line.
Oh, I get it now. My brain just went flop during the process.

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.