Sora Teichman
Sora Teichman

Reputation: 184

Can ChoETL csv reader read a column into an ICollection field?

I have a csv with details about an order. There is one line per order, but in my code this translates to an Order object with an ICollection<OrderLine> Lines property. I need to read each order in to an Order object with the order line info added as an item to the Lines collection.

I am sending in a column mapping dictionary for the reader to use.

_orderMapping = new Dictionary<string, string>
            {
                {"Customer", "CustomerName"},
                {"Model Number", "Lines.ElementAt(0).ModelNumber"},
                {"Ship Date", "ShipDate"},
                {"Quantity", "Lines.ElementAt(0).Quantity"},
                {"Warehouse", "Lines.ElementAt(0).WarehouseName"}
            };

Here's how I set up the ChoCSVRecordConfiguration:

var config = new ChoCSVRecordConfiguration<T>()
                .WithFirstLineHeader()
                .Configure(c => c.ThrowAndStopOnMissingField = false)
                .Configure(c => c.IgnoreEmptyLine = true)
                .Configure(c => c.FileHeaderConfiguration.IgnoreColumnsWithEmptyHeader = true);

            foreach (var header in headers)
            {
                if (mapping.TryGetValue(header, out var propName))
                    config.Map(propName, header);
            }

where mapping is my mapping dictionary and headers are the csv sheet headers. I tried using Lines[0] and Lines.ElementAt(0), but Lines is still null. Is there a way to do this?

A sample csv:

sample csv

Code for Order object:

public class Order : OrderWithoutLines
    {
        public ICollection<OrderLine> Lines { get; set; }
    }

public class OrderLine
    {
    public string Id { get; set; }
    public int ParentId { get; set; }
    public string OrderNumber { get; set; }
    public int ModelId { get; set; }
    public string ModelNumber { get; set; }
    public string CustomerItemNumber { get; set; }
    public int Quantity { get; set; }
    public int WarehouseId { get; set; }
    public string WarehouseName { get; set; }
    public decimal? Price { get; set; }
    public string Status { get; set; }
    public string State { get; set; }
    public string FulfilledByPO { get; set; }
}

Order inherits from OrderWithoutLines which contains the rest of the Order info.

Upvotes: 0

Views: 171

Answers (1)

Sora Teichman
Sora Teichman

Reputation: 184

Thanks @Cinchoo for your help; straight from the source of the ChoETL library! This is not possible out of the box. I ended up using a CsvOrder class to import my orders with an OrderLine child class, and then I convert them to Order objects.

Upvotes: 0

Related Questions