简体   繁体   中英

Create List of object from a complex CSV field

Really not trying to create "hacky" code here but I'm really unsure on the best approach.

My company has took on a new feature to use CSV as a data source for our less technical clients, whilst the majority is very straight forward, we have a couple of fields that may potentially be the down fall...

Now at the moment I have a field "products" which has a header value of:

ID|Name|Color|Size|Quantity|RetailPrice|Net|Tax|Total|ProductImage|Brand|Custom_Field_1|Custom_Field_2|Custom_Field_3|Custom_Field_4|Custom_Field_5|Custom_Field_6|Custom_Field_7|Custom_Field_8|Custom_Field_9|Custom_Field_10|Custom_Field_11|Custom_Field_12|Custom_Field_13|Custom_Field_14|Custom_Field_15|Custom_Field_16|Custom_Field_17|Custom_Field_18|Custom_Field_19|Custom_Field_20

And in this field, it can contain multiple products, all separated via "|" after some preliminary testing, I've created a collection of string with 38 values, 38 values corresponding with the individual items in the header value.

Field value: 68977|Lara|||1|245.00|225|20.00|245.00|/uploads/images/products/image_main/lara.jpg|Bundle MacLaren|||||||||||||||||||||68978|Gazelle|||1|375.00|355|20.00|375.00|/uploads/images/products/image_main/gazelle.jpg|Cara Meehan London|||||||||||||||||||||

As you can see some values can be "empty" but the never the less still breaks of via splitting.

I'm really struggling to figure out the best approach to create a robust block of code.

Sample block of code:

     public static List<Model.DataAPI.Product> ModelToCollection(CSVOrder source,
                                                        string shipment_id)
     {
        var collection = new List<Model.DataAPI.Product>();
        if (!string.IsNullOrWhiteSpace(source.Products))
         {
            //ID|Name|Color|Size|Quantity|RetailPrice|Net|Tax|Total|ProductImage|Brand|Custom_Field_1|Custom_Field_2|Custom_Field_3|Custom_Field_4|Custom_Field_5|Custom_Field_6|Custom_Field_7|Custom_Field_8|Custom_Field_9|Custom_Field_10|Custom_Field_11|Custom_Field_12|Custom_Field_13|Custom_Field_14|Custom_Field_15|Custom_Field_16|Custom_Field_17|Custom_Field_18|Custom_Field_19|Custom_Field_20

             var productStringValues = Strings.SplitString(source.Products, "|".ToCharArray());
              var count = productStringValues.Count;

          } 
     }

The only thing that springs to mind, is creating a collection in multiples of 38, but it feels really "hacky".

Any ideas?

Much Appreciated,

It looks like you are retrieving the 'Products' as a single string of data items delimited by a pipe symbol.

However, what you are looking at when you get the 'Products' is a delimited list of 'Products' that have their data items delimited by a pipe symbol. When you take all of this data (all of the 'Products' in the field) and split using the pipe symbol, you will get a massive list of data items for all of the 'Products' - not for each one.

Note the subtle difference here; you need to retrieve each 'Product' by its delimiter (maybe a carriage return, maybe something else) to obtain a list of 'Products'. Then for each 'Product' you can use the pipe delimiter to get each of the 38 data items it has.

Then you can add each 'Product' to your List.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM