2012-04-14 170 views
1

我是新來的,在這裏已經很久了,我無法解決問題。我有下面的代碼在我ShowAllReview控制器:用數據庫的不同記錄搜索可以搜索

public ActionResult Index(string Ordering, string WordFilter, string DisplaySearchResults, int? CounterForPage) 
     { 
      using (var db = new gamezoneDBEntities()) 
      { 

       ViewBag.Message = TempData["message"]; 
       ViewBag.CurrentSort = Ordering; 
       ViewBag.NameSortParm = String.IsNullOrEmpty(Ordering) ? "GameName" : ""; 
       ViewBag.DateSortParm = Ordering == "ReleaseYearOfGame" ? "DiscriptionOfGame" : "Date"; 


       { 
        TempData["DisplaySearchResult"] = DisplaySearchResults; 

        { 
         ViewBag.search = DisplaySearchResults; 
        } 
        if (Request.HttpMethod == "GET") 
        { 
         DisplaySearchResults = WordFilter; 
        } 
        else if (DisplaySearchResults == "") 
        { 
         ViewData["MyMessage"] = "Nothing Has Been Entered."; 

        } 

        else 
        { 
         CounterForPage = 1; 
        } 

        ViewBag.CurrentFilter = DisplaySearchResults; 


        var FullDatabaseItem = from b in db.tblReviews.Include(x => x.tblGame) 
              select b; 



        if (!String.IsNullOrEmpty(DisplaySearchResults)) 
        { 

         FullDatabaseItem = FullDatabaseItem.Include (x => x.tblGame) 
         .Where (b => b.Score.ToUpper().Contains(DisplaySearchResults.ToUpper())); 

        } 

        switch (Ordering) 
        { 
         case "HeadlineName": 
          FullDatabaseItem = FullDatabaseItem.OrderBy(b => b.Score); 
          break; 
         case "DatePosted": 
          FullDatabaseItem = FullDatabaseItem.OrderBy(b => b.Recomendation); 
          break; 
         case "DiscriptionDate": 
          FullDatabaseItem = FullDatabaseItem.OrderBy(b => b.Recomendation); 
          break; 
         default: 
          FullDatabaseItem = FullDatabaseItem.OrderByDescending(b => b.Recomendation); 
          break; 
        } 

        int pageSize = 3; 
        int pageNumber = (CounterForPage ?? 1); 
        var PageNumberResults = FullDatabaseItem.ToPagedList(pageNumber, pageSize); 
        ViewBag.PageNumberResults = FullDatabaseItem.Count(); 
        if (PageNumberResults.Any()) 
        { 

         return View(PageNumberResults); 
        } 

        return View("ErrorView"); 
       } 
      } 
     } 

,你可以看到我已經添加以下代碼:隨着出包括在審查表

var FullDatabaseItem = from b in db.tblReviews.Include(x => x.tblGame)select b; 

認爲會扔了以下錯誤:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. 

現在的問題是這行代碼:

FullDatabaseItem = FullDatabaseItem.Include (x => x.tblGame) 
         .Where (b => b.Score.ToUpper().Contains(DisplaySearchResults.ToUpper())); 

我試圖獲得遊戲名稱,所以當用戶使用我的搜索時,他可以獲得一個只需輸入遊戲名稱的記錄購買。但正如你所看到的正在搜索的字段是"Score"這是tblReview字段,因爲我需要的字段"GameName"在那裏不起作用,因爲它表示它不存在。

我需要幫助解決問題

任何您需要了解更多信息請諮詢我,我應提供

謝謝

回答

1

你的實體模型從tblReview到tblGame導航屬性,正確?所以你有一個看起來像這樣的實體?

public class tblReview 
{ 
    public virtual tblGame tblGame { get; set; } 
    // other properties 
} 

這種關係是雙向的還是單向的?意思是說,你的tblGame類有這樣的tblReviews集合屬性嗎?

public class tblGame 
{ 
    public virtual ICollection<tblReview> tblReviews { get; set; } 
    // other properties 
} 

如果是這樣,那麼你有一個雙向關聯,並且可以在一個實體從場中的其他實體搜索領域。

舉例來說,您想顯示具有特定名稱的遊戲的評論列表。你能做到這一點,像這樣:

string gameName = "Pac-Man"; 
using (var db = new gamezoneDBEntities()) 
{ 
    // get reviews for game named pac-man 
    var reviews = db.tblReviews.Include(r => r.tblGame) 
     .Where(r => r.tblGame.GameName.Equals(gameName, 
      StringComparison.OrdinalIgnoreCase)); 

    // get game with reviews scored greater than 4 
    var games = db.tblGames.Include(g => g.tblReviews) 
     .Where(g => g.tblReviews.Any(r => r.Score > 4)); 
} 

所以,最後,我想這是你所追求的:

var FullDatabaseItem = db.tblReviews.Include(g => g.tblGame); 
if (!string.IsNullOrEmpty(DisplaySearchResults)) 
{ 
    FullDatabaseItem = FullDatabaseItem 
     .Where(review => review.tblGame.GameName.Contains(DisplaySearchResults)); 
} 

我不認爲你需要將字符串轉換上爲了做在使用.Contains()時不區分大小寫。搜索「pAC-mAN」仍然應該返回名爲「Pac-Man」的遊戲。

+0

我剛剛收到這個答案會試圖暗示它到我的,看看我得到什麼結果。將提醒ou的結果,謝謝你的回答 – user1319420 2012-04-14 13:39:37

+0

非常感謝你的工作:)你最好的我一直在這個很長時間,並得到它的工作:) – user1319420 2012-04-14 13:51:37