我创建了一个变量,它返回数据库中的菜单 ID 列表。然后,我将该变量放入另一个查询中,该查询在列表中返回膳食。我的问题是我一直收到错误 Cannot implicitly convert type System.Collections.Generic.List<int?>
至 int?
var getmenuids = (from r in db.Menus
where (r.Restaurantid == currentrestaurant)
select r.Id).ToList();
var meals = db.Meals.Where(r => r.MenuID = getmenuids).ToList();
最佳答案
您遇到问题是因为您使用的是 ==
(将单个元素与列表进行比较)而不是 Contains()
(搜索 an 元素在列表中)。
var meals = db.Meals.Where(r => getmenuids.Contains(r.MenuID)).ToList();
你也可以将它们结合起来......像这样:
var meals = (from meal in db.Meals
join menu in db.Menus on meal.MenuID equals menu.Id
where menu.Restaurantid == currentrestaurant
select meal).ToList();
关于c# - 查询 Linq 中的列表变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22494029/