简体   繁体   English

在实体框架中,您将如何对联接表进行“ IN”查询?

[英]In the entity framework, how would you do an “IN” query on a joined table?

I'm a SQL junkie, and the syntax of the EF is not intuitive to me. 我是一名SQL迷,EF的语法对我来说并不直观。

I have a Restaurant table and a Food table. 我有一个餐厅桌子和一个食物桌子。 I want the restaurants and foods where the foods have a type contained in the string list Categories. 我想要在字符串列表类别中包含食物类型的餐馆和食物。 Here is some SQL that roughly represents what I want. 这是一些可以大致代表我想要的SQL。

SELECT r.*, f.*
FROM Restaurant R
  JOIN food f on f.RestaurantID = r.RestaurantID
WHERE f.Type IN ("Awesome", "Good", "Burrito")

Here's the code I want to turn into that SQL. 这是我想变成该SQL的代码。

List<string> types = new List<string>() { "Awesome", "Good", "Burrito"};
var dbrestaurants = from d in db.Restaurants
                    .Include("Food")
                    //where Food.Categories.Contains(types)//what to put here?
                    select d;

尝试

var restaurants = db.Restaurants.Where(r => types.Contains(r.Food.Type));
where Food.Categories.Any(c => types.Contains(c.Name))

Try this: 尝试这个:

var dbRestaurants = 
from r in db.Restaurants
join f in db.Foods on r.RestaurantId equals f.RestaurantId
where types.Any(foodType => foodType == f.Type)
select r;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM