英文:
c# list and csv file reading
问题
你好,这是我在C# .NET Core 6中的代码:
using (var reader = new StreamReader(@"C:\Users\ho3in\source\repos\dataset\data\orders.csv"))
{
using (var Csvreader = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var records = Csvreader.GetRecords<datamodel>().ToList();
// 你想要访问数据并处理它,例如打印数据、按用户搜索等。
}
}
public class datamodel
{
public int ID_Order { get; set; }
public int ID_Customer { get; set; }
public int ID_Item { get; set; }
public DateTime DateTime_CartFinalize { get; set; }
public float Amount_Gross_Order { get; set; }
public string city_name_fa { get; set; }
public float Quantity_item { get; set; }
}
我想访问我的数据并进行操作,比如打印数据,按用户搜索等等。
英文:
hello guys this is my code in C# .netcore 6
using (var reader = new StreamReader(@"C:\Users\ho3in\source\repos\dataset\data\orders.csv"))
{
using (var Csvreader = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var records = Csvreader.GetRecords<datamodel>().ToList();
}
}
public class datamodel
{
public int ID_Order { get; set; }
public int ID_Customer { get; set; }
public int ID_Item { get; set; }
public DateTime DateTime_CartFinalize { get; set; }
public float Amount_Gross_Order { get; set; }
public string city_name_fa { get; set; }
public float Quantity_item { get; set; }
}
i want to print my data but im just gettin `dataset.datamodel' printed out how can i access the data and work with it btw im using the csvhelper lib
i want to access my data and work with it such as print the data search by user etc..
答案1
得分: 0
这是您的类型的对象列表。
var records = Csvreader.GetRecords<datamodel>().ToList();
如果您想打印它,请使用您的类的属性:
foreach(var record in records)
{
Console.WriteLine($"{record.ID_Customer}, {record.ID_Item}, {record.DateTime_CartFinalize}");
}
您也可以像stuartd提到的那样覆盖ToString方法:
public class datamodel
{
// 属性和其他内容
public override string ToString()
{
return $"{ID_Customer}, {ID_Item}, {DateTime_CartFinalize}";
}
}
英文:
Here you have a list of objects of your type.
var records = Csvreader.GetRecords<datamodel>().ToList();
If you want to print it, then use properties of your class:
foreach(var record in records)
{
Console.WriteLine($"{record.ID_Customer}, {record.ID_Item}, {record.DateTime_CartFinalize}");
}
You can also override ToString method as stuartd mentioned:
public class datamodel
{
// properties and others
public override string ToString()
{
return $"{ID_Customer}, {ID_Item}, {DateTime_CartFinalize}";
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论