private void WriteResultToFile(IList <ItemInfo> itemInfos) { var directoryName = Path.GetDirectoryName(SourceFilePath); if (directoryName == null) { return; } var fileName = Path.GetFileNameWithoutExtension(SourceFilePath); var targetFilePath = Path.Combine(directoryName, $"{fileName}. result {DateTime.Now:yyyy-MM-dd hh-mm-ss}.csv"); using (var csvWriter = new CsvWriter(targetFilePath)) { csvWriter.Write("Всего"); csvWriter.Write("Общие излишки"); csvWriter.AddNewLine(); csvWriter.Write(itemInfos.Count); var totalSurplus = itemInfos.Select(ItemInfoHelper.GetFreeLength) .Sum(); csvWriter.Write(totalSurplus); csvWriter.AddNewLine(); csvWriter.Write("Номер"); csvWriter.Write("Излишки"); csvWriter.AddNewLine(); for (int i = 0; i < itemInfos.Count; i++) { var itemInfo = itemInfos[i]; csvWriter.Write(i + 1); csvWriter.Write(ItemInfoHelper.GetFreeLength(itemInfo)); foreach (var itemLength in itemInfo.TargetItemLengths) { csvWriter.Write(itemLength); } csvWriter.AddNewLine(); } } }