无法使用不同的数据库记录进行搜索

时间:2012-04-14 01:37:54

标签: c# asp.net asp.net-mvc-3 asp.net-mvc-3-areas

我是新来的,多年来一直在这里,我无法解决问题。我的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;

如果没有评论表中的include,则视图会抛出以下错误:

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 个答案:

答案 0 :(得分: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”仍应返回名为“吃豆人”的游戏。