Reading CSV file and storing values into an array

后端 未结 19 1445
猫巷女王i
猫巷女王i 2020-11-22 06:35

I am trying to read a *.csv-file.

The *.csv-file consist of two columns separated by semicolon (\";\").

I am able

19条回答
  •  名媛妹妹
    2020-11-22 07:14

    look at this

    using CsvFramework;

    using System.Collections.Generic;

    namespace CvsParser {

    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List Orders { get; set; }        
    }
    
    public class Order
    {
        public int Id { get; set; }
    
        public int CustomerId { get; set; }
        public int Quantity { get; set; }
    
        public int Amount { get; set; }
    
        public List OrderItems { get; set; }
    
    }
    
    public class Address
    {
        public int Id { get; set; }
        public int CustomerId { get; set; }
    
        public string Name { get; set; }
    }
    
    public class OrderItem
    {
        public int Id { get; set; }
        public int OrderId { get; set; }
    
        public string ProductName { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
    
            var customerLines = System.IO.File.ReadAllLines(@"Customers.csv");
            var orderLines = System.IO.File.ReadAllLines(@"Orders.csv");
            var orderItemLines = System.IO.File.ReadAllLines(@"OrderItemLines.csv");
    
            CsvFactory.Register(builder =>
            {
                builder.Add(a => a.Id).Type(typeof(int)).Index(0).IsKey(true);
                builder.Add(a => a.Name).Type(typeof(string)).Index(1);
                builder.AddNavigation(n => n.Orders).RelationKey(k => k.CustomerId);
    
            }, false, ',', customerLines);
    
            CsvFactory.Register(builder =>
            {
                builder.Add(a => a.Id).Type(typeof(int)).Index(0).IsKey(true);
                builder.Add(a => a.CustomerId).Type(typeof(int)).Index(1);
                builder.Add(a => a.Quantity).Type(typeof(int)).Index(2);
                builder.Add(a => a.Amount).Type(typeof(int)).Index(3);
                builder.AddNavigation(n => n.OrderItems).RelationKey(k => k.OrderId);
    
            }, true, ',', orderLines);
    
    
            CsvFactory.Register(builder =>
            {
                builder.Add(a => a.Id).Type(typeof(int)).Index(0).IsKey(true);
                builder.Add(a => a.OrderId).Type(typeof(int)).Index(1);
                builder.Add(a => a.ProductName).Type(typeof(string)).Index(2);
    
    
            }, false, ',', orderItemLines);
    
    
    
            var customers = CsvFactory.Parse();
    
    
        }
    }
    

    }

提交回复
热议问题