简体   繁体   中英

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.

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.

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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