export - Exporting class containing an ObservableCollection to csv in C# -


i've found many solutions exporting class csv problem this:

the class i'm trying export has property observablecollection. eg:

public class shipmentforexport     {         public string waybillnumber { get; set; }         public datetime waybilldate { get; set; }         public string customername { get; set; }         public string customercode { get; set; }         public string collectingbranchname { get; set; }         public string collectingbranchcode { get; set; }         public string recipientname { get; set; }         public string recipientphonenumber { get; set; }         public string recipientcellphonenumber { get; set; }         public string recipientcompany { get; set; }         public string destinationaddress1 { get; set; }         public string destinationaddress2 { get; set; }         public string destinationcity { get; set; }         public string destinationsuburb { get; set; }         public string destinationprovince { get; set; }         public string destinationcountry { get; set; }         public string destinationpostalcode { get; set; }          ***public observablecollection<inhouseparcel> parcels { get; set; }***      } 

when try export list of shipments csv works parcels not export way want them to.

i have tried using filehelpers library , csvhelper well.

any appreciated!!

using csvhelper...

when writing, you'll have write manually because writing of collection properties isn't supported.

foreach( var record in records ) {     csv.writefield( record.waybillnumber );     ...     foreach( var parcel in record.parcels )     {         csv.writefield( parcel );     } } 

reading little easier because can add in mapping.

map( m => m.parcels ).convertusing( row => {     var oc = new observablecollection<inhouseparcel>();     var parcel = row.getfield<inhouseparcel>( 17 );     oc.add( parcel ); } ); 

you'll need convert field values inhouseparcel , loop through remainder of fields in row. i'll leave task you.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -