String.Join format and how to break to a new line...

yguyon28
yguyon28 used Ask the Experts™
on
I have the following String.Join that I'm creating a csv export from but I'm having dificulty formating to a new line when it come the  Done By property String.Join(",", x1.StationActivity.SelectMany( y => y.ProductsUsed).Select(z => z.DoneBy))?? string.Empty,

I have included a screen shoot in what I'm trying to accomplish with my sample code.

           
                headerNames1.Add(ServiceAssistant.Tools.Language.Translate(Tools.Language.Keys.Product_Code, ViewBag));
                headerNames1.Add(ServiceAssistant.Tools.Language.Translate(Tools.Language.Keys.Description, ViewBag));
                headerNames1.Add(ServiceAssistant.Tools.Language.Translate(Tools.Language.Keys.Target, ViewBag));
                headerNames1.Add(ServiceAssistant.Tools.Language.Translate(Tools.Language.Keys.Done, ViewBag));
                var headers1 = String.Join(",", headerNames1.ToArray());

                sb.AppendLine(headers1);
                foreach (var x1 in (List<StationHistoryReportResults>)TempData["excsv"])

                {
                    var data1 = String.Join(",", new string[] {
                        //string.Format("\"{0}\"",item.B_Company),
                       
                      String.Join(",", x1.StationActivity.SelectMany( y => y.ProductsUsed).Select(z => z.ProductCode))?? string.Empty,
                      String.Join(",", x1.StationActivity.SelectMany( y => y.ProductsUsed).Select(z => z.ProductTranslatedDescription))?? string.Empty,
                      String.Join(",", x1.StationActivity.SelectMany( y => y.ProductsUsed).Select(z => z.Target))?? string.Empty,
                      String.Join(",", x1.StationActivity.SelectMany( y => y.ProductsUsed).Select(z => z.DoneBy))?? string.Empty,
                     });

                    sb.AppendLine(data1);

                }

                

                csv = sb.ToString();
                return File(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv", "StationExportData.csv");
            }
            else

Open in new window


Captureexporttocsv.PNG
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Darrell PorterEnterprise Business Process Architect

Commented:
After the
String.Join(",", x1.StationActivity.SelectMany( y => y.ProductsUsed).Select(z => z.DoneBy))?? string.Empty,

Open in new window

consider adding
String.Join("\n"),

Open in new window

Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
var data1 = String.Join(",", new string[] {
                        //string.Format("\"{0}\"",item.B_Company),
                       
    String.Join(",", x1.StationActivity.SelectMany( y => y.ProductsUsed).Select(z => z.ProductCode))?? string.Empty,
    String.Join(",", x1.StationActivity.SelectMany( y => y.ProductsUsed).Select(z => z.ProductTranslatedDescription))?? string.Empty,
    String.Join(",", x1.StationActivity.SelectMany( y => y.ProductsUsed).Select(z => z.Target))?? string.Empty,
    String.Join(",", x1.StationActivity.SelectMany( y => y.ProductsUsed).Select(z => z.DoneBy))?? string.Empty,
});

Open in new window

I'm no C# Expert, but when I see something like this ........ too many things done at once.
Maybe consider splitting this kind of expression into something more simple.

Writing code isn't only about achieving the desired results,  it is first about writing something understandable, that make sens, since the code is supposed to be readable by humans beeing.

Author

Commented:
I'm going to try this...

Add an empty space after sb.AppendLine(data1);?

i.e.

sb.Append(" ");
or maybe

sb.Append(Environment.NewLine);
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
no luck....
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
Simplify your code so you clearly understand what is going on (as it stand, you don't !!), add more variables
toggle a breakpoint, and inspect your variables with the debugger.

Author

Commented:
Thank you for the advice.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial