2016-05-31 85 views
0

代碼看起來如下:如何組織重複代碼?

1)我有一些圖片:

var imageIds = caseStudyPage.Images; 
foreach (var imageId in imageIds) 
{ 
    byte[] csImage = GetImage(imageId); 
} 

2),我有結構如下:

var document = new Document(); 
document 
    .AddField(new TextField("CS Counter Basic Name", "Case Study")) 
    .AddField(new TextField("CS Counter Number", _numberInParentDocument.ToString())) 
    .AddField(new TextField("Page Title", caseStudyPage.Page_Title)) 
    .AddField(new TextField("Common Footer Text 1", commonFooterText1)) 
    .AddField(new TextField("Page Title Content", caseStudyPage.Page_Title)) 
    .AddTable(new Table("Images") 
     .AddRow(new TableRow().AddField(new ImageField("Image", imageBytes, ImageType.Jpeg)) 
      .AddField(new TextField("Overview", caseStudyPage.Overview)) 
      .AddField(new TextField("Engagement", caseStudyPage.Engagement)) 
      .AddField(new TextField("Publish Date", caseStudyPage.Publish_Date.ToString("MMM. dd, yyyy"))) 
    .AddField(new TextField("Common Footer Text 2", commonFooterText2)))); 

我需要把文檔結構從2)從1)的循環內部,以設置ImageField的imageBytes變量,但文檔應該只有一個。我不知道循環只有這部分如何:

.AddRow(new TableRow().AddField(new ImageField("Image", imageBytes, ImageType.Jpeg)) 

回答

0
var document = new Document(); 
document 
    .AddField(new TextField("CS Counter Basic Name", "Case Study")) 
    .AddField(new TextField("CS Counter Number", _numberInParentDocument.ToString())) 
    .AddField(new TextField("Page Title", caseStudyPage.Page_Title)) 
    .AddField(new TextField("Common Footer Text 1", commonFooterText1)) 
    .AddField(new TextField("Page Title Content", caseStudyPage.Page_Title)); 

var table = new Table("Images"); 
foreach (var imageId in imageIds) 
{ 
    byte[] csImage = GetImage(imageId); 
    table.AddRow(new TableRow().AddField(new ImageField("Image", csImage, ImageType.Jpeg)) 
      .AddField(new TextField("Overview", caseStudyPage.Overview)) 
      .AddField(new TextField("Engagement", caseStudyPage.Engagement)) 
      .AddField(new TextField("Publish Date", caseStudyPage.Publish_Date.ToString("MMM. dd, yyyy"))); 
} 

document.AddTable(table); 
document.AddField(new TextField("Common Footer Text 2", commonFooterText2)))); 
+0

有一個錯誤: 嚴重\t代碼\t說明\t項目\t文件\t線\t抑制狀態 錯誤\t CS1061 \t「文件」不不包含'AddRow'的定義,並且沒有找到接受類型'Document'的第一個參數的擴展方法'AddRow'(可以找到缺少使用指令或程序集引用嗎?)\t EPAM.Lookbook.Sitecore \t C:\項目\ EPM-lkbk的\ src \ EPAM.Lookbook.Sitecore \ CMS \ PDF \ NewPdfBuilder.cs 主動 – tesicg

+0

我已經找到了解決辦法: IEnumerable的

表= document.Tables; tables.ElementAt(0).AddRow(new TableRow()。AddField(new ImageField(「Image」,csImage,ImageType.Jpeg)) – tesicg