3

Visual Studio 2012 で Entity Framework コードの最初の手法を使用しています ここに私のコンテキストがあります

public class BakingWebContext : DbContext
{
        public DbSet<Recipe> Recipes { get; set; }
        public DbSet<Category> Categories { get; set; }
}

私はカテゴリクラスを持っています

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; }

}

レシピの仮想コレクションが含まれています

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; }
}

C# でラムダ式を使用して、 IsCompanyRecipeがtrueとマークされているレシピのみを含むすべてのカテゴリを返そうとしています。

これまでのところ、私はこれを持っています

var query = categories.Where(c => c.Recipes.Count > 0).SelectMany(r => r.Recipes.Where(re => re.IsCompanyRecipe == true));

すべての会社のレシピのリストを返しますが、すべてのレシピを含むリストをIEnumerable<Recipe>返したいのですが、どこですか?IEnumerable<Category>IsCompanyRecipe == true

4

1 に答える 1

5
var query = (from c in categories
         from r in c.Recipes
         where r.IsCompanyRecipe == true
         select c);
于 2012-12-24T14:22:46.733 に答える