title: Simplify process with linq query
body: <p>This is my Table:</p>
  
  <p><strong>PupilNutrition</strong></p>
  
  <pre><code>Id     PupilId   NutritionId
  1       10          100  
  2       10          101
  </code></pre>
  
  <p>My another table <strong>Nutrition</strong>:</p>
  
  <pre><code>Id  Nutritioncategory  BatchId   NutritionRate   NutritionId   Operation
  1       A                1         9000             100          1
  2       B                1         5000             100          0
  3       C                1         5000             100          1
  4       D                2         6000             101          2           
  5       E                2         7000             101          2         
  6       F                2         8000             101          0          
  </code></pre>
  
  <p>This is one field to store final <strong>Rate</strong>:</p>
  
  <pre><code>decimal Rate= 0;
  </code></pre>
  
  <p><strong>Case 1</strong>: <strong>Operation field with Value 0 and Batch Id 1</strong></p>
  
  <pre><code> Rate= Rate + NutritionRate(i.e 5000 because for batch id 1 with condition 0 only 1 record is there).
  </code></pre>
  
  <p><strong>Case 2</strong>:<strong>Now for operation field with Value 1 and Batch Id 1</strong></p>
  
  <pre><code>Here i want to sum up both the value i.e(10000 and 5000) but during addition if sum of both is greater than 10000 then just take 10000 else take sum like this:
  if(9000 + 5000 &gt; 10000)
    Rate= 10000
  else
      Rate=Rate + (Addition of both if less than 10000).
  </code></pre>
  
  <p><strong>Case 3</strong>:<strong>Now for operation field with Value 0 and Batch Id 2</strong></p>
  
  <pre><code> Rate= Rate + NutritionRate(i.e 8000 because for batch id 1 with condition 0 only 1 record is there).
  </code></pre>
  
  <p><strong>Case 4</strong>:<strong>Now for operation field with Value 2 and Batch Id 2</strong></p>
  
  <pre><code>Here I will select Maximum value from Nutrition Rate field if there are 2 records:
  Rate=Rate - (Maximum value from 6000 and 7000 so will take 7000).
  </code></pre>
  
  <p>My Class file:</p>
  
  <pre><code> public partial class PupilNutrition
      {
          public int Id { get; set; }
          public int PupilId { get; set; }
          public int NutritionId { get; set; }
      }
  
   public partial class Nutrition
      {
          public int Id { get; set; }
          public string Nutritioncategory { get; set; }
          public decimal NutritionRate  { get; set; }
          public Nullable&lt;int&gt; NutritionId { get; set; }
      }
  </code></pre>
  
  <p>This is how i have Done:</p>
  
  <pre><code>batchId=1 or 2;//
   List&lt;int&gt; NutritionId = db.PupilNutrition.Where(x =&gt; PupilId  == 10).Select(c =&gt; c.NutritionId).ToList();  //output 100,101
   var data = db.Nutrition.Where(x =&gt; x.BatchId == batchId &amp;&amp; NutritionId.Contains(x.NutritionId.Value)).ToList();
  </code></pre>
  
  <p>I have tried like this but its a labour process:</p>
  
  <pre><code>var data1=data.where(t=&gt;t.Operation==0);
  Rate= Rate + data1.Sum(p =&gt; p.NutritionRate);
  </code></pre>
  
  <p>Similarly for Operation 1:</p>
  
  <pre><code>var data2=data.where(t=&gt;t.Operation==1);
  This is left because here i need to sum up 2 value if two records are there as shown in my above cases and if sum is greater than 10000 than select 10000 else select sum.
  </code></pre>
  
  <p>Similarly for Operation 2:</p>
  
  <pre><code>var data3=data.where(t=&gt;t.Operation==2);
  Rate= Rate - data3.Max(p =&gt; p.NutritionRate);
  </code></pre>
  
  <p>I think i have done a labour process as this can be done even in better way with linq query only i guess.</p>
  
  <p>So can anybody help me to simplify this whole process in linq query or even some better way and provide solution for Operation Value 2 which is left?</p>