Search code examples
c#stringcsvobject

Convert object to CSV string


I am having problems converting an object of products into a csv string.

var data = ProductPresenter.ExportAllProducts();
string csv = String.Join(",", data.Select(x => x.ToString()).ToArray());

This produces an output of:

BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts,BLL.Views.ExportAllProducts

Expected output would be like:

Small Hat, 12.10, true
Medium Hat, 13.50, true
Large Hat, 15.00, false
... 

What conversion am I missing to get the values out of each item on the object?


Solution

  • Your data variable is not a list of strings, either add a .ToString() method to that class or create a list of strings and use that.