Last active
December 13, 2018 07:25
-
-
Save poychang/cca886ccb8b72357c8db52faf254ffb5 to your computer and use it in GitHub Desktop.
[將 CSV 轉成 Model] 用最簡單的方式將 CSV 轉成 Model,但要注意 CSV 是否有標頭,以及轉型的問題 #dotnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
//var content = File.ReadAllLines("YOUR_FILE.csv"); | |
var content = new string[] { "2018/12/13,Data1,100", "2018/12/14,Data2,200", "2018/12/14,Data3,300" }; | |
var models = content | |
.Select(p => p.Split(',')) | |
.Select(p => new Model | |
{ | |
Date = DateTime.Parse(p[0]), | |
Data = p[1], | |
Number = Convert.ToInt32(p[2]) | |
}) | |
.Dump(); | |
} | |
class Model | |
{ | |
public DateTime Date { get; set; } | |
public string Data { get; set; } | |
public int Number { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment