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"不会在那里工作,因为它说它不存在。

我需要帮助来解决问题

您需要任何更多信息,请询问我,我会提供

谢谢你

4

1 回答 1

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”的游戏。

于 2012-04-14T02:40:03.613 回答