I am using Entity Framework code first technique in Visual Studio 2012 Here is my context
public class BakingWebContext : DbContext
{
public DbSet<Recipe> Recipes { get; set; }
public DbSet<Category> Categories { get; set; }
}
I have a Category Class
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; }
}
It contains a virtual collection of recipes
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; }
}
I am trying to return all the categories including only recipes that have the IsCompanyRecipe marked as true using Lambda expression in C#
So far I've got this
var query = categories.Where(c => c.Recipes.Count > 0).SelectMany(r => r.Recipes.Where(re => re.IsCompanyRecipe == true));
which returns an IEnumerable<Recipe> list of all the company recipes but I want to return an IEnumerable<Category> list including all the recipes where IsCompanyRecipe == true?
var query = (from c in categories
from r in c.Recipes
where r.IsCompanyRecipe == true
select c);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With