我有两个问题:
成分:
IngId
Description
可用成分:
IngId
我已经有了一个配料查询员:
var ingQuery = from i in context.Ingredients
select i;
我怎样才能给他的添加一个连接,以便它按
AvailableIngredient
(即内部连接)?如果我必须一直加入,我知道怎么做,也就是说,从…加入上下文。可用…但是连接是有条件的,所以我需要使用其他语法:
if (filterByAvailable)
{
IQueryable<Available> availableQuery = GetAvailableIngredientQuery(context);
ingQuery = ingQuery.Join(...); // Can I use this to join to the query?
}
这可能不是正确的方法,所以我想这样做:
-
GetAvailableEdientQuery返回
可用成分查询,即
3000/6000(但没有
把结果列举出来
从ef返回作为iqueueryable)
-
将availablequery联接到ingquery,这样两个查询之间就有一个内部联接。
编辑:
这是我当前使用的代码(非常快),但它意味着重复的代码:
IQueryable<Ingredient> query;
if (filterByAvailable)
{
IQueryable<Available> availableQuery = GetAvailableIngredientQuery(context);
query = from item in context.Ingredients
// Quite a few `where` clauses and stuff
join t in availableQuery on item.IngId equals t.IngId
select item;
}
else
{
query = from item in context.Ingredients
// The SAME `where` clauses and stuff as above
select item;
}