Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to cache a value evaluated in a lambda expression?
    text
    copied!<p>In the ContainsIngredients method in the following code, is it possible to cache the <em>p.Ingredients</em> value instead of explicitly referencing it several times? This is a fairly trivial example that I just cooked up for illustrative purposes, but the code I'm working on references values deep inside <em>p</em> eg. <em>p.InnerObject.ExpensiveMethod().Value</em></p> <p>edit: I'm using the PredicateBuilder from <a href="http://www.albahari.com/nutshell/predicatebuilder.html" rel="nofollow noreferrer">http://www.albahari.com/nutshell/predicatebuilder.html</a></p> <pre><code>public class IngredientBag { private readonly Dictionary&lt;string, string&gt; _ingredients = new Dictionary&lt;string, string&gt;(); public void Add(string type, string name) { _ingredients.Add(type, name); } public string Get(string type) { return _ingredients[type]; } public bool Contains(string type) { return _ingredients.ContainsKey(type); } } public class Potion { public IngredientBag Ingredients { get; private set;} public string Name {get; private set;} public Potion(string name) : this(name, null) { } public Potion(string name, IngredientBag ingredients) { Name = name; Ingredients = ingredients; } public static Expression&lt;Func&lt;Potion, bool&gt;&gt; ContainsIngredients(string ingredientType, params string[] ingredients) { var predicate = PredicateBuilder.False&lt;Potion&gt;(); // Here, I'm accessing p.Ingredients several times in one // expression. Is there any way to cache this value and // reference the cached value in the expression? foreach (var ingredient in ingredients) { var temp = ingredient; predicate = predicate.Or ( p =&gt; p.Ingredients != null &amp;&amp; p.Ingredients.Contains(ingredientType) &amp;&amp; p.Ingredients.Get(ingredientType).Contains(temp)); } return predicate; } } [STAThread] static void Main() { var potions = new List&lt;Potion&gt; { new Potion("Invisibility", new IngredientBag()), new Potion("Bonus"), new Potion("Speed", new IngredientBag()), new Potion("Strength", new IngredientBag()), new Potion("Dummy Potion") }; potions[0].Ingredients.Add("solid", "Eye of Newt"); potions[0].Ingredients.Add("liquid", "Gall of Peacock"); potions[0].Ingredients.Add("gas", "Breath of Spider"); potions[2].Ingredients.Add("solid", "Hair of Toad"); potions[2].Ingredients.Add("gas", "Peacock's anguish"); potions[3].Ingredients.Add("liquid", "Peacock Sweat"); potions[3].Ingredients.Add("gas", "Newt's aura"); var predicate = Potion.ContainsIngredients("solid", "Newt", "Toad") .Or(Potion.ContainsIngredients("gas", "Spider", "Scorpion")); foreach (var result in from p in potions where(predicate).Compile()(p) select p) { Console.WriteLine(result.Name); } } </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