SWi*_*lko 3 .net c# linq lambda entity-framework
我在Visual Studio 2012中使用实体框架代码第一技术这是我的上下文
public class BakingWebContext : DbContext
{
public DbSet<Recipe> Recipes { get; set; }
public DbSet<Category> Categories { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有一个分类
public class Category
{
[Key]
public int CategoryId { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
public string ImageUrl { get; set; }
public virtual ICollection<Recipe> Recipes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
它包含一个虚拟的食谱集合
public class Recipe
{
[Key]
public int RecipeId { get; set; }
[Required]
public string Title { get; set; }
public string Description { get; set; }
public bool IsCompanyRecipe { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我试图返回所有类别,包括仅使用C#中的Lambda表达式将IsCompanyRecipe标记为true的食谱
到目前为止我已经有了这个
var query = categories.Where(c => c.Recipes.Count > 0).SelectMany(r => r.Recipes.Where(re => re.IsCompanyRecipe == true));
Run Code Online (Sandbox Code Playgroud)
它返回IEnumerable<Recipe>所有公司食谱的列表,但我想返回一个IEnumerable<Category>包含所有食谱的列表IsCompanyRecipe == true?
var query = (from c in categories
from r in c.Recipes
where r.IsCompanyRecipe == true
select c);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
257 次 |
| 最近记录: |