djd*_*d87 14 linq join entity-framework-4 ef-code-first c#-4.0
我有两个IQueryables:
成分:
IngId
Description
Run Code Online (Sandbox Code Playgroud)
AvailableIngredient:
IngId
Run Code Online (Sandbox Code Playgroud)
我已经有了一个IQueryable成分:
var ingQuery = from i in context.Ingredients
select i;
Run Code Online (Sandbox Code Playgroud)
如何向他添加AvailableIngredient联接以便过滤(即内部联接)?如果我必须一直加入,我知道如何做到这一点,即从...加入context.Available ...等),但是Join是有条件的,所以我需要使用其他语法:
if (filterByAvailable)
{
IQueryable<Available> availableQuery = GetAvailableIngredientQuery(context);
ingQuery = ingQuery.Join(...); // Can I use this to join to the query?
}
Run Code Online (Sandbox Code Playgroud)
这可能不是正确的方法,所以这就是我想要做的:
编辑:
这是我目前使用的代码(非常快),但它意味着重复的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
Cli*_*ity 19
使用第一个查询作为后续查询的来源.
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21024 次 |
| 最近记录: |