title: Linq expression to call Contains method with nullable parameter
body: <p>I have database column</p>
  
  <pre><code>SelectListId int null
  </code></pre>
  
  <p>And I have complicated filter on web page, which I am trying to transfer to <code>LinqToSql</code> in order to get filtered data from database.</p>
  
  <p>I have several expressions which works, but one I struggle with.</p>
  
  <p>I would like to call something like this <code>x =&gt; SelectedIdsByUser.Contains(x.SelectListId)</code></p>
  
  <p>So I have a function which returns predicate</p>
  
  <pre><code>// this function works for 'SelectListId int not null' columns
  public static Expression&lt;Func&lt;T, bool&gt;&gt; SelectListContainsPredicate&lt;T&gt;(string columnName, List&lt;int&gt; searchValues)
  {
        var type = typeof(T);
        ParameterExpression parameter = Expression.Parameter(type, "x");
        ConstantExpression constant = Expression.Constant(true);
  
        // When column is invalid, return true
        PropertyInfo property = type.GetProperties().FirstOrDefault(p =&gt; p.Name == columnName);
  
        if (property == null || searchValues.Count == 0)
        {
            return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(constant, parameter);
        }
  
        // Define expression :
        // x =&gt; SearchValues.Contains(x.Column)
  
        MemberExpression member = Expression.Property(parameter, property);
  
        MethodInfo method = typeof(List&lt;int&gt;).GetMethod("Contains");
  
        constant = Expression.Constant(searchValues);
  
        // Here it throws :
        // System.ArgumentException: Expression of type 'System.Nullable`1[System.Int32]' 
        // cannot be used for parameter of type 'System.Int32' of method 'Boolean Contains(Int32)'
        // Because: column is int? and List&lt;int&gt;.Contains(int?) doesn't work.
        Expression expression = Expression.Call(constant, method, member);
  
        return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(expression, parameter);
  }
  </code></pre>
  
  <p>but I am getting an error, because <code>SelectListId</code> is <code>Nullable&lt;int&gt;</code> but <code>Contains</code> method has only <code>int</code> parameter. What can I do here, any Idea ?</p>
  
  <blockquote>
    <p>System.ArgumentException: Expression of type
    'System.Nullable`1[System.Int32]'  cannot be used for parameter of
    type 'System.Int32' of method 'Boolean Contains(Int32)'</p>
  </blockquote>