Skip to content

Instantly share code, notes, and snippets.

@JudahGabriel
Created July 1, 2016 20:54
Show Gist options
  • Save JudahGabriel/fef01e0b0784927ba256f318726a170f to your computer and use it in GitHub Desktop.
Save JudahGabriel/fef01e0b0784927ba256f318726a170f to your computer and use it in GitHub Desktop.
// Theoretical LINQ code to perform a JOIN against a relational database.
public RecipeViewModel GetRecipeDetails(string recipeId)
{
var recipeViewModel = from recipe in dbContext.Recipes
where recipe.Id == 42
join chef in dbContext.Chefs on chef.Id equals recipe.ChefId
let ingredients = dbContext.Ingredients.Where(i => i.RecipeId == recipe.Id)
let comments = dbContext.Comments.Where(c => c.RecipeId == recipe.Id)
let categories = dbContext.Categories.Where(c => c.RecipeId == recipeId)
select new RecipeViewModel
{
RecipeId = recipe.Id
Name = recipe.Name,
PictureUrl = recipe.PictureUrl,
Categories = categories.Select(c => c.Name).ToList(),
ChefEmail = chef.Email,
ChefName = chef.Name,
Ingredients = ingredients.ToList(),
Comments = comments.ToList()
};
return recipeViewModel;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment