Help how to improve this code/make it more compact
Hi!
I am looking for ways to improve this block of code, similar kind of code is found throughout the application I am working on, and I wonder if there is a way to compact it, make it more readable and less error prone, maybe through some clever lablda syntax, extension methods etc?
Code:
private void CopyToExcelExecute() {
StringBuilder sb = new StringBuilder();
Clipboard.Clear();
foreach (var row in SelectedBOMRows) {
if (row.BOMRow.Position != null)
sb.Append(row.BOMRow.Position.ToString());
else
sb.Append(";");
sb.Append(";");
sb.Append(row.BOMRow.Level.ToString());
sb.Append(";");
if (row.BOMRow.ArticleReference != null) {
sb.Append(row.BOMRow.ArticleReference.Object.ArticleNumber);
sb.Append(";");
sb.Append(row.BOMRow.ArticleReference.Object.Title.DisplayText);
sb.Append(";");
sb.Append(row.BOMRow.ArticleReference.Quantity.ToString());
sb.Append(";");
}
else
sb.Append(";;;");
sb.Append(row.BOMRow.Remark.DisplayText);
if (row.BOMRow.SectionReference != null) {
sb.Append(";");
sb.Append(row.BOMRow.SectionReference.Object.Title.ToString());
}
else
sb.Append(";");
sb.Append(Environment.NewLine);
}
Clipboard.SetText(sb.ToString(), TextDataFormat.CommaSeparatedValue);
}
I appreciate all assistance and input! Most of the code is boilerplate and I really wish I could remove the if/else checks and write some compact C# code instead...
/S