title: C# LINQ: Efficient way to set anonymous type property based on other values in query?
body: <pre><code>// The "myData" table contains two fields: name, value and type.
  // The "myInfo" table contains two fields: type and value.
  
  var myQuery = from x in myData
      select new {
          name = x.name,
          value = x.value,
          minValue = myInfo.Where(y =&gt; x.type == y.type).OrderBy(y =&gt; y.value).FirstOrDefault().value,
          isOverMinValue = (myInfo.Where(y =&gt; x.type == y.type).OrderBy(y =&gt; y.value).FirstOrDefault().value) &gt; x.value ? true : false
      };
  </code></pre>
  
  <p>The myInfo table might contain multiple records matching the type from myData; I only want the minimum value. Then I want the isOverMinValue field to indicate whether the value in myData is greater than the minimum value found in myInfo.</p>
  
  <p>In this example, it works, but as you can see I have to repeat the entire subquery twice. This can't be efficient. The alternative approach I had was:</p>
  
  <pre><code>var myQuery = from x in myData
      select new {
          name = x.name,
          value = x.value,
          minValue = myInfo.Where(y =&gt; x.type == y.type).OrderBy(y =&gt; y.value).FirstOrDefault().value,
          isOverMinValue = false
      };
  
  foreach (var myRec in myQuery)
  {
      if (myRec.minValue &lt; x.Value) myRec.isOverMinValue = true;
  }
  </code></pre>
  
  <p>This however won't work because it seems the properties on anonymous types are read-only.</p>
  
  <p>Is there a more efficient way to do this?</p>
  
  <p>(I can't do a join because like I said myInfo might contain multiple entries, and doing a normal LINQ join will end up producing many records based on how many matches are found in myInfo.)</p>