Hello,
I have a LINQ to EF query that returns data in a class form. The class has a List<RecipeCategories>
property that I need to populate. The RecipeCategories table is a relationship table between the Recipe table and the RecipeCategories table, and can be many to many. I found enough information to get the code to compile, but it errors at runtime and I haven't been able to figure out how to get this right.
ri = (from r in recipeData.Recipes
where r.ID == recipeId
select new RecipeItem
{
Id = r.ID,
ProductId = r.Product.ID,
RecipeName = r.recipeName,
RecipeDescription = r.recipeDescription,
Servings = r.servings.HasValue ? r.servings.Value : 0,
CreatedDate = r.createdDate,
PrepTime = r.prepTime.HasValue ? r.servings.Value : 0,
CookTime = r.cookTime.HasValue ? r.servings.Value : 0,
Approved = r.approved,
RecipeInstructions = r.recipeInstructions,
RecipeIngredients = r.recipeIngredients,
RecipeCategories = r.RecipeCategories.Select(i => new RecipeCategoryItem { Id = i.ID, CategoryName = i.categoryName }).ToList()
}).First();
This is the error I get.
LINQ to Entities does not recognize the method 'System.Collections.Generic.List
1[RecipeCategoryItem] ToList[RecipeCategoryItem](System.Collections.Generic.IEnumerable
1[RecipeCategoryItem])' method, and this method cannot be translated into a store expression.
The part that I am working on is this line.
RecipeCategories = r.RecipeCategories.Select(i => new RecipeCategoryItem { Id = i.ID, CategoryName = i.categoryName }).ToList()
RecipeCategories is a List<RecipeCategoryItem>
property.
Is what I am trying to do possible, and if so, how?
Thank you.