2016-06-13 78 views
3

我遇到了我的代碼問題。我試圖通過ID 在我的表中選擇行任何幫助真的很感激它。謝謝。不能將類型'...'隱式轉換爲'System.Collections.Generic.IList <...>'。存在明確的轉換(您是否缺少演員?)

public FileStreamResult GetPdf(int? id) 
{ 
     IList<Requisition> all = new List<Requisition>(); 
     using (ClinlabEntities dc = new ClinlabEntities()) 
     { 
      all = dc.Requisitions.ToList().First(ReqId => ReqId.ReqId == id); 
     } 
     WebGrid grid = new WebGrid(source: all, canPage: false, canSort: false); 
     string gridHtml = grid.GetHtml(
     columns: grid.Columns(
     grid.Column("FirstName", ""), 
     grid.Column("LastName", " "), 
      //grid.Column("AccountNo", ""), 
     grid.Column("Address", ""), 
     grid.Column("City", ""), 
     grid.Column("State", ""), 
     grid.Column("Zip", "") 
     )).ToString(); 

     string exportData = String.Format("<html><head>{1}</head><body>{1}</body></html>", "<style>table(border-spacing:10px; border-collapse:seperate;)</style>", gridHtml); 
     var bytes = System.Text.Encoding.UTF8.GetBytes(exportData); 
     using (var input = new MemoryStream(bytes)) 
     { 
      var output = new MemoryStream(); 
      var document = new iTextSharp.text.Document(PageSize.LETTER, 15, 0, 0, -15); 
      var writer = PdfWriter.GetInstance(document, output); 
      writer.CloseStream = false; 
      document.Open(); 
      var xmlWorker = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance(); 
      xmlWorker.ParseXHtml(writer, document, input, System.Text.Encoding.UTF7); 
      document.Close(); 
      output.Position = 0; 
      return new FileStreamResult(output, "Application/Pdf"); 
     } 
     } 
    } 
} 
+0

你有額外的'}}'在你的代碼也一定要仔細檢查代碼格式在.cs文件以及 – MethodMan

回答

2

要指定您的申請表到all變量,它期待List<Requisition>而不是從.First()返回Requisition的第一個結果。

IList<Requisition> all = new List<Requisition>(); 
using (ClinlabEntities dc = new ClinlabEntities()) { 
    all = dc.Requisitions.ToList().First(ReqId => ReqId.ReqId == id); 
} 

最簡單的解決辦法是改變all期待一個記錄:

Requisition all = null; 

然而,這是令人難以置信的低效率,因爲你實際加載所有的Requisition記錄到內存中,然後過濾。您應該改用.FirstOrDefault()和放棄的.ToList()

Requisition all = null; 
using (ClinlabEntities dc = new ClinlabEntities()) { 
    all = dc.Requisitions.FirstOrDefault(ReqId => ReqId.ReqId == id); 
} 
+0

非常感謝你的幫助。我按照您的建議進行了更改。但它給了我一個不同的錯誤:( – Frank

+0

@Frank不是一個問題,我建議你用修改後的代碼和錯誤開一個新的問題,這樣你的問題就可以清晰明瞭。 –

相關問題