title: How to return from ConditionalExpression.IfThen in tree with MethodCallExpression / lambda?
body: <p>I am trying to get an expression tree to conditionally evaluate to a string.</p>
  
  <p>Here is my code thus far:</p>
  
  <pre><code>IQueryable&lt;Category&gt; myCategories = DataUtil.Categories.AsQueryable();
  
  ParameterExpression categoryParameterExpression = Expression.Parameter(typeof (Category), "category");
  MemberExpression categoryNameMemberExpression = Expression.PropertyOrField(categoryParameterExpression, "CategoryName");
  MemberExpression categoryNameLengthExpression = Expression.Property(categoryNameMemberExpression, typeof (string).GetProperty("Length"));
  ConstantExpression constantLengthExpression = Expression.Constant(10, typeof (int));
  BinaryExpression greaterThanLengthExpression = Expression.GreaterThan(categoryNameLengthExpression, constantLengthExpression);
  var getNameInCapsMethod = typeof (Category).GetMethod("GetNameInCaps", BindingFlags.Instance | BindingFlags.Public);
  MethodCallExpression getNameInCapsExpression = Expression.Call(categoryParameterExpression, getNameInCapsMethod, categoryNameMemberExpression);
  
  ConditionalExpression ifGreaterThanLengthGetUpperNameExpression = Expression.IfThen(greaterThanLengthExpression, getNameInCapsExpression); 
  
  // I need something between the lambda and the ConditionalExpression to ensure that the void type is not returned?
  
  var ifGreaterThanLengthGetUpperNameLambdaExpression = Expression.Lambda&lt;Func&lt;Category, string&gt;&gt;(ifGreaterThanLengthGetUpperNameExpression, new ParameterExpression[] { categoryParameterExpression }); 
  foreach (var category in myCategories)
  {
      var upperName = ifGreaterThanLengthUpperLambda(category);
      System.Windows.MessageBox.Show(upperName);
  }
  </code></pre>
  
  <p>Here is the ArgumentException that is occuring at runtime:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.ArgumentException' occurred in
    System.Core.dll</p>
    
    <p>Additional information: Expression of type 'System.Void' cannot be
    used for return type 'System.String'</p>
  </blockquote>
  
  <p>I have figured out that ConditionalExpression is returning the void type for the "IfFalse" condition. </p>
  
  <p>Here is the screenshot from my Expression Tree Visualizer:
  <img src="https://i.stack.imgur.com/4Rw6I.png" alt="ConditionalExpression returning void"></p>
  
  <p>I just want the string value. I realize that there is an Expression.IfThenElse on the ConditionalExpression type, but I'm not sure what to put in the Else expression. (I don't want to just pass back an empty string, if possible.) Is there a way for me to ensure that the condition is only evaluated when the preceding BinaryExpression evaluates to true? Regardless, how do I solve this problem?</p>