にロードされる
public class ResultsModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Phone { get; set; }
public string Username { get; set; }
public bool Sucess { get; set; }
public string MessageType { get; set; }
public string SenderMessageSent { get; set; }
public string SenderMessageReceived { get; set; }
}
です値の文字列。 (コレクションから= 1つのオブジェクト1本のライン)
public static IEnumerable<string> ToCsv<T>(IEnumerable<T> list)
{
var fields = typeof(T).GetFields();
var properties = typeof(T).GetProperties();
foreach (var @object in list)
{
yield return string.Join(",",
fields.Select(x => (x.GetValue(@object) ?? string.Empty).ToString())
.Concat(properties.Select(p => (p.GetValue(@object, null) ?? string.Empty).ToString()))
.ToArray());
}
}
そしてexamplary使用法:あなたは、ループの書き方を
var oemResultsModels = new List<OemResultsModel>
{
new OemResultsModel
{
FirstName = "Fname1",
LastName = "LName1",
MessageType = "Type1",
Phone = 1234567,
SenderMessageReceived = "something1",
SenderMessageSent = "somethingelse1",
Sucess = true,
Username = "username1"
},
new OemResultsModel
{
FirstName = "Fname2",
LastName = "LName2",
MessageType = "Type2",
Phone = 123456789,
SenderMessageReceived = "something2",
SenderMessageSent = "somethingelse2",
Sucess = false,
Username = "username2"
}
};
using (var textWriter = File.CreateText(@"C:\destinationfile.csv"))
{
foreach (var line in ToCsv(oemResultsModels))
{
textWriter.WriteLine(line);
}
}
を求めていますか? –
@EdPlunkettええ、私はOPがループを書いていると思います。 – AVK
@AVKNaiduまあ、ここの誰かが秘密を見つけたと確信しています。 –