title: Microsoft Solver Foundation - how to use function as constraint?
body: <p>I'm trying to find a solution to a problem using Microsoft Solver Foundation in C# and I'm having trouble setting up all the constraints I need. My basic model is I have a list of bays and I need to load each bay so that the total of all the bays is maximised. I'm currently doing it like this</p>
  
  <pre><code>var solver = SolverContext.GetContext();
  var model = solver.CreateModel();
  var decisions = 
      bays.Select(b =&gt; new Decision(Domain.IntegerNonnegative, "B"+b.bay.getShortName()));
  model.AddDecisions(decisions.ToArray());
  
  foreach (BayPositionLoading bay in bays)
  {
      model.AddConstraint(
              "B" + bay.bay.getShortName() + "Cons",
              model.Decisions
                   .First(d =&gt; d.Name == "B" + bay.bay.getShortName()) &lt;= bay.bay.maxLoad);
  }
  </code></pre>
  
  <p>What I really would like to be able to do is add a constraint that a certain function returns true. The function would be something like this</p>
  
  <pre><code>public bool isValid (List&lt;Bay&gt; bays)
  {
      return blah;
  }
  </code></pre>
  
  <p>But I can't figure out how to create the list of bays to pass to this function. I would like to do something like but this keeps throwing an exception when I say ToDouble or GetDouble. </p>
  
  <pre><code>foreach(Bay b in bays)
  {
      var dec= model.Decisions.First(it =&gt; it.Name == "B" + bay.bay.getShortName());
      b.actualLoad = dec.ToDouble(); // Or GetDouble
  }
  model.AddConstraint("func", isValid(bays) == true);
  </code></pre>
  
  <p>Can anyone suggest how this can be done? 
  Thanks!</p>