2012-02-01 50 views
0

即時通訊非常新的MVC3,我負責添加一個搜索框到我的MVC3控制器,並使控制器顯示結果基於從數據庫搜索。MVC LinQ搜索錯誤

我的代碼粘貼在下面,拋出一個錯誤,錯誤信息也被粘貼。

public ViewResult Index(string searchString) 
    { 
     var newsItem = from n in db.NewsItems 
         select n; 
     if (!String.IsNullOrEmpty(searchString)) 
     { 
      newsItem = newsItem.Where(n => n.Posted.ToUpper().Contains(searchString.ToUpper()) 
         || n.Posted.ToUpper().Contains(searchString.ToUpper())); 

     } 


     return View(db.NewsItems.ToList()); 
    } 

錯誤:

'System.DateTime' does not contain a definition for 'ToUpper' and no extension method 'ToUpper' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?)
C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\Web_Assignment\Web_Assignment\Controllers\NewsController.cs 27 40 Web_Assignment

+1

這功課嗎?你的錯誤是相當具有描述性的。 – 2012-02-01 19:11:10

回答

2

我認爲財產發佈有DateTyme類型。那麼如果你真的想在DateTime列中搜索 - 你需要將它轉換爲字符串類型:newsItem.Where(n => n.Posted.ToString().ToUpper().Contains(searchString.ToUpper()) 但是,也許你真的想在Text屬性中搜索?

newsItem.Where(n => n.Text.ToUpper().Contains(searchString.ToUpper())