无法使用数据库的不同记录进行搜索
本文关键字:记录 搜索 数据库 | 更新日期: 2023-09-27 18:31:00
我是新来的,已经在这里工作了很长时间,我无法解决问题。我的ShowAllView控制器中有以下代码:
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"
在那里不起作用,因为它说它不存在。
我需要帮助来解决问题
您需要更多信息,请问我,我将提供
谢谢
您的实体模型具有从 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"仍应返回名为"吃豆人"的游戏。