C# 列表和 CSV 文件读取

huangapple go评论128阅读模式
英文:

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(@&quot;C:\Users\ho3in\source\repos\dataset\data\orders.csv&quot;))
{
    using (var Csvreader = new CsvReader(reader, CultureInfo.InvariantCulture))
    {
        var records = Csvreader.GetRecords&lt;datamodel&gt;().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

C# 列表和 CSV 文件读取

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&lt;datamodel&gt;().ToList();

如果您想打印它,请使用您的类的属性:

foreach(var record in records)
{
    Console.WriteLine($&quot;{record.ID_Customer}, {record.ID_Item}, {record.DateTime_CartFinalize}&quot;);
}

您也可以像stuartd提到的那样覆盖ToString方法:

public class datamodel
{
    // 属性和其他内容

    public override string ToString()
    {
        return $&quot;{ID_Customer}, {ID_Item}, {DateTime_CartFinalize}&quot;;
    }
}
英文:

Here you have a list of objects of your type.

var records = Csvreader.GetRecords&lt;datamodel&gt;().ToList();

If you want to print it, then use properties of your class:

foreach(var record in records)
{
    Console.WriteLine($&quot;{record.ID_Customer}, {record.ID_Item}, {record.DateTime_CartFinalize}&quot;);
}

You can also override ToString method as stuartd mentioned:

public class datamodel
{
    // properties and others

    public override string ToString()
    {
        return $&quot;{ID_Customer}, {ID_Item}, {DateTime_CartFinalize}&quot;;
    }
}

huangapple
  • 本文由 发表于 2023年4月11日 08:04:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定