用于查找食谱的存储过程或 SQL 查询,相关配料列表包含整个传递的字符串列表

Stored-procedure or SQL-query to find recipes, which related ingredients list contains whole passed list of strings

我正在使用 .net、mssql、LinqToSQL。我的 mssql 数据库具有以下架构 - 食谱与成分相关。现在我想让用户通过几种成分找到食谱。 我需要一个可用于 select 所有这些食谱的存储过程或查询。 搜索成分数量未知。

我尝试使用 lintosql

var list = datacontext.Recipes.Where(r => values.All(v => r.RecipeIngredientMeasures.Any(i => i.Ingredient.NameTrans == v))).ToList();

其中 values 是我搜索的成分数组。

但后来我得到了

"Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator."

这是我在这种情况下使用的 SQL 查询。

declare @ingredNames table (name nvarchar(255));

-- so you need to split the list of ingredient comma separated string to table
-- to do that take a look to the link in the comments
insert into @ingredNames (name) values ('rice'), ('chicken');

-- get recipes that have all the specified ingredients
select r.Id, r.Name as number from Recipies as r 
inner join RecipeIngredientMeasures as rim on rim.RecipeId= r.Id
inner join Ingredients as ing on rim.IngId= ing.Id

inner join @ingredNames as ingN on ingN.name = ing.NameTrans 
Group By r.Id, r.Name
Having count(ing.Id) = (select count(name) from @ingredNames )

祝你好运。