Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use let to define a new set of data within a LINQ query?
    text
    copied!<p>I have a class that represents all the Ingredients for a Recipe</p> <pre><code>public class ViewRecipe { public string RecipeName { get; set; } public string IngredientName { get; set; } public double UnitWeight { get; set; } public double TotalWeight { get; set; } public ViewRecipe() { } public ViewRecipe(string _RecipeName, string _IngredientName, double _UnitWeight, double _TotalWeight) { RecipeName = _RecipeName; IngredientName = _IngredientName; UnitWeight = _UnitWeight; TotalWeight = _TotalWeight; } }(in reality there are a LOT more data members like quantity, weight, etc....) </code></pre> <p>The actual dataset is a List called ViewRecipeSummary and it looks like this:</p> <pre><code>RecipeName IngredientName 1 A 1 B 1 C 2 D 2 E 3 A 3 Z </code></pre> <p>I have an existing query which finds an INGRIDENT (recipeGroup.Key) and I need to now do the following:</p> <p>1- find all recipes that have that ingredient</p> <p>2- return all rows from my data for those recipes</p> <p>And I need to then use this inside my query (so it needs to be with a LET or something)</p> <p>Lets assume I am looking for all recipes that share ingredient A, I expect the end result of my LET (based on the data I show above) to look exactly like this:</p> <pre><code>RecipeName IngredientName 1 A 1 B 1 C 3 A 3 Z </code></pre> <p>So for any recipe that has ingredient A (1 and 3) return all rows of those recipes so I have all the ingredients I need. And this should be in the same form as my ViewRecipe class (not some new {RecipeName, List}, etc...), it should provide the same rows of data in the same format.</p> <p>Current LINQ Query:</p> <pre><code> ViewFullRecipeGrouping = ( from data in ViewRecipeSummary group data by data.RecipeName into recipeGroup let fullIngredientGroups = recipeGroup.GroupBy(x =&gt; x.IngredientName) select new ViewFullRecipe() { RecipeName = recipeGroup.Key, RecipeIngredients = ( from ingredientGroup in fullIngredientGroups select new GroupIngredient() { IngredientName = ingredientGroup.Key, } ).ToList(), ViewGroupRecipes = ( // THIS IS WHERE I NEED TO ADD MY CODE SO THAT I CAN USE THE RESULT BELOW let a = ..... // BUT I HAVE NO CLUE HOW TO PERFORM SUCH A QUERY HERE select new GroupRecipe() { // USE THE RESULTS FOUND TO GENERATE MY RECIPE GROUP // BASED ON THE RESULTS FOUND ABOVE RecipeName = a.key }).ToList(), }).ToList(); </code></pre> <p>Something like:</p> <pre><code> let a = ViewRecipeSummary.GroupBy(x =&gt; x.RecipeName) .Where(g =&gt; g.Any(x =&gt; x.IngredientName == recipeGroup.Key)) .Select(g =&gt; new ViewRecipe() { RecipeName = g.Key, IngredientName = g.Select(x =&gt; x.IngredientName) }) </code></pre> <p>But this returns a List and I need to return it broken down into the same structure.</p> <p>Here are the classes used:</p> <pre><code> public class GroupRecipe { public string RecipeName { get; set; } public List&lt;GroupIngredient&gt; Ingredients { get; set; } public GroupRecipe() { } public GroupRecipe(string _recipeName, List&lt;GroupIngredient&gt; _ingredients) { RecipeName = _recipeName; Ingredients = _ingredients; } } public class GroupIngredient { public string IngredientName { get; set; } public double UnitWeight { get; set; } public double TotalWeight { get; set; } public GroupIngredient() { } public GroupIngredient(string _IngredientName, double _UnitWeight, double _TotalWeight) { IngredientName = _IngredientName; UnitWeight = _UnitWeight; TotalWeight = _TotalWeight; } } public class ViewFullRecipe { public string RecipeName { get; set; } public List&lt;GroupIngredient&gt; RecipeIngredients { get; set; } public List&lt;GroupRecipe&gt; ViewGroupRecipes { get; set; } public ViewFullRecipe() { } public ViewFullRecipe(string _RecipeName, List&lt;GroupIngredient&gt; _RecipeIngredients, List&lt;GroupRecipe&gt; _ViewGroupRecipes) { RecipeName = _RecipeName; RecipeIngredients = _RecipeIngredients; ViewGroupRecipes = _ViewGroupRecipes; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload