所以我的功能在我的控制器中生成pdf。但是,我无法在pdf中打印该集合,因为它给了我System.Collections.Generic.HashSet 我想打印hashset的内容。有什么想法可以解决这个问题吗?
private static byte[] GeneratePdf(List<Plant> plants)
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (var doc = new Document())
{
PdfWriter.GetInstance(doc, memoryStream);
doc.Open();
doc.SetMargins(120, 120, 270, 270);
BaseFont font = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Font normalFont = new Font(font, 12, Font.NORMAL, BaseColor.BLACK);
Paragraph pgTitle = new Paragraph();
pgTitle.Font = new Font(font, 20, Font.NORMAL, BaseColor.BLACK);
pgTitle.Add("American University of Beirut");
doc.Add(pgTitle);
Paragraph pgPlantTitle = new Paragraph();
pgPlantTitle.Font = new Font(font, 18, Font.NORMAL, BaseColor.BLACK);
pgPlantTitle.Add("Plant Description");
doc.Add(pgPlantTitle);
foreach (Plant p in plants)
{
Paragraph plantDisc = new Paragraph();
plantDisc.Font = new Font(font, 14, Font.NORMAL, BaseColor.BLACK);
plantDisc.Add(new Paragraph(" "));
plantDisc.Add(new Paragraph(" "));
plantDisc.Add("Plant Name :");
plantDisc.Add(p.ScientificName);
plantDisc.Add(new Paragraph(" "));
plantDisc.Add("Plant Type :");
plantDisc.Add(new Paragraph(" "));
plantDisc.Add(p.TypeOfPlants.ToString());
plantDisc.Add("Plant Height Range :");
plantDisc.Add(new Paragraph(" "));
plantDisc.Add("Plant Spread Range :");
plantDisc.Add(new Paragraph(" "));
plantDisc.Add("Tree Shape :");
plantDisc.Add(new Paragraph(" "));
plantDisc.Add("Plant Origin :");
plantDisc.Add(new Paragraph(" "));
plantDisc.Add("Plant Color :");
plantDisc.Add(new Paragraph(" "));
plantDisc.Add("Fruit Color :");
plantDisc.Add(new Paragraph(" "));
plantDisc.Add("Light Requirement :");
List<string> list = p.LightRequirements.Select(z => z.ToString()).ToList();
plantDisc.Add(list.ToString());
plantDisc.Add(new Paragraph(" "));
plantDisc.Add("Water Requirement :");
doc.Add(plantDisc);
doc.Add(new Paragraph(" "));
}
}
return memoryStream.ToArray();
}
}
答案 0 :(得分:1)
我无法从你的Hast Set所在的代码中解决,但是如果你在集合上执行.ToString(),那么你将得到的是类型:
var mySet = new HashSet<string>();
mySet.Add("Lorem");
mySet.Add("Ipsum");
System.Console.WriteLine(mySet.ToString());
// OUTPUTS: System.Collections.Generic.HashSet`1[System.String]
您需要执行以下操作:
foreach (var thing in mySet)
{
System.Console.WriteLine(thing);
}
// OUTPUTS: Lorem
// Ipsum
您可以将其作为.net fiddle运行,以了解我的意思。
根据您想要输出的内容,您可以做一些像Linq .Aggregate命令那样更整洁的事情。