簡體   English   中英

在實體框架中,您將如何對聯接表進行“ IN”查詢?

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

我是一名SQL迷,EF的語法對我來說並不直觀。

我有一個餐廳桌子和一個食物桌子。 我想要在字符串列表類別中包含食物類型的餐館和食物。 這是一些可以大致代表我想要的SQL。

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

這是我想變成該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))

嘗試這個:

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