我有两个IQueryables:
成分:
IngId
Description
AvailableIngredient:
IngId
我已经有了一个IQueryable成分:
var ingQuery = from i in context.Ingredients
select i;
如何向他添加联接,以便按AvailableIngredient
过滤(即内部联接)?如果我必须一直加入,我知道如何做到这一点,即从...加入context.Available ......等等,但是Join是有条件的,所以我需要使用其他语法:
if (filterByAvailable)
{
IQueryable<Available> availableQuery = GetAvailableIngredientQuery(context);
ingQuery = ingQuery.Join(...); // Can I use this to join to the query?
}
这可能不是正确的方法,所以这就是我想要做的事情:
修改
这是我目前正在使用的代码(非常快),但它意味着重复的代码:
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;
}
答案 0 :(得分:18)
使用第一个查询作为后续查询的来源。
IQueryable<Ingredient> query = from item in context.Ingredients
// Quite a few `where` clauses and stuff
select item;
if (filterByAvailable)
{
IQueryable<Available> availableQuery = GetAvailableIngredientQuery(context);
query = from item in query
join t in availableQuery on item.IngId equals t.IngId
select item;
}