c# - 查询 Linq 中的列表变量

标签 c# asp.net linq

我创建了一个变量,它返回数据库中的菜单 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/

相关文章:

c# - 应用完全SOA?

c# - 在 Windows 窗体应用程序中嵌入 Teamviewer

c# - ASP.Net MVC 3 登录和 Windows 身份验证

c# - 使用 LINQ 拆分集合

c# - 如何在字典中查找值> 0且键与特定字符串模式匹配的键值对?

c# - 基本 ASP.NET MVC 3 查询

c# - mvc4 actionlink 传递变量

c# - 在函数内执行存储过程而不等待返回

asp.net - Internet Explorer 中的asp.net 表单例份验证问题

Linq To Entities 获取列表中的倒数第二个条目