2015-07-20 91 views
-1

我得到以下錯誤消息爲什麼String.HtmlEncode無法識別?

LINQ實體無法識別方法System.String HtmlEncode(System.String)方法,該方法不能 翻譯成商店表達。

使用這個LINQ的聲明

using (TranEntities model = new TranEntities()) 
{ 
     studentNames = (from source in model.Students 
         where source.Name.Contains(pre) 
         select System.Web.HttpUtility.HtmlEncode(source.Name.Replace(pre, "<span style='color : red'>" + pre + "</span>"))).ToList(); 
} 
+0

你的問題是什麼? –

+0

很明顯,這個問題涉及錯誤消息的解釋和解決方案。 – showdev

回答

1

你需要從LINQ到實體到步驟LINQ到對象,使用AsEnumerable方法,像這樣:

using (TranEntities model = new TranEntities()) 
{ 
    var studentNamesE = (from source in model.Students 
        where source.Name.Contains(pre) 
        select source).AsEnumerable(); 

    studentNames = (from source in studentNamesE 
        select System.Web.HttpUtility 
          .HtmlEncode(source.Name.Replace(pre, 
             "<span style='color : red'>" + pre + "</span>"))) 
       .ToList(); 
} 
相關問題