我正在尝试将 RestaurantReservationEventsTbl 中的 RestaurantId 与 RestaurantTbl 中的 RestaurantID 相关联以显示仅对当前登录的餐厅进行的预订。
我在代码中收到以下错误 operator ==无法应用于int类型的操作数和iqueryable int
以下是我在家庭控制器中所做的事情
var RestaurantIDRestaurantTbl = from r in db.Restaurants select r.RestaurantID;
//var listOfRestaurantsReservations = db.RestaurantReservationEvents.ToList();
var listOfRestaurantsReservations = db.RestaurantReservationEvents.Where(x => x.RestaurantID == RestaurantIDRestaurantTbl).ToList();
//return View(restaurants.Where(x => x.RestaurantEmailAddress == UserEmail).ToList());
//create partial view called _RestaurantReservation
return PartialView("_RestaurantReservations", listOfRestaurantsReservations);
答案 0 :(得分:1)
您必须更改代码才能实现restaurantIds
,如下所示:
var RestaurantIDRestaurantTbl = (from r in db.Restaurants
select r.RestaurantID).ToList();
然后您可以更改下面的代码以进行比较:
var listOfRestaurantsReservations = db.RestaurantReservationEvents.Where(x => RestaurantIDRestaurantTbl.Contains(x.RestaurantID)).ToList();
无论如何,这不是最好的解决方案。我会为你写另一个例子,试试这个例子是否有效,让我知道结果。
我会考虑更改下面的代码以提高效率:
var listOfRestaurantsReservations = (from r in db.Restaurants
join e in db.RestaurantReservationEvents
on r.RestaurantID equals e.RestaurantID
//where r.RestaurantID == something //if where condition needed
select e).ToList();
如果你的表没有与外键相关联,请考虑阅读本文档here以更好地构建表格,因为它们彼此相关。
如果您的表格与文档文章相关,那么您可能会有类似的内容:
var RestaurantIDRestaurantTbl = db.Restaurants.SingleOrDefault(x => x.RestaurantID == something);
if(RestaurantIDRestaurantTbl != null)
{
var listOfRestaurantsReservations = RestaurantIDRestaurantTbl.RestaurantReservationEvents.ToList();
}
答案 1 :(得分:0)
{
// This will give you a list of IDs
var RestaurantIDRestaurantTbl = db.Restaurants
.Select(p => p.RestaurantID)
.ToList();
// Using .Any() is a better choice instead of .Contains()
// .Contains is used to check if a list contains an item while .Any will look for an item in a list with a specific ID
var listOfRestaurantsReservations = db.RestaurantReservationEvents
.Where(p => RestaurantIDRestaurantTbl.Any(r => r.pRestaurantID == p))
.ToList();
}