C# 要调用的Linq表达式包含参数为null的方法

C# 要调用的Linq表达式包含参数为null的方法,c#,linq-to-sql,linq-expressions,C#,Linq To Sql,Linq Expressions,我有一个数据库列 SelectListId int null 我在网页上有一个复杂的过滤器,我正试图将它传输到LinqToSql,以便从数据库中获得过滤后的数据 我有好几种表达方式,但有一种我难以接受 我想调用类似这样的东西x=>selectedsdsbyuser.Contains(x.SelectListId) 所以我有一个返回谓词的函数 // this function works for 'SelectListId int not null' columns public static

我有一个数据库列

SelectListId int null
我在网页上有一个复杂的过滤器,我正试图将它传输到
LinqToSql
,以便从数据库中获得过滤后的数据

我有好几种表达方式,但有一种我难以接受

我想调用类似这样的东西
x=>selectedsdsbyuser.Contains(x.SelectListId)

所以我有一个返回谓词的函数

// this function works for 'SelectListId int not null' columns
public static Expression<Func<T, bool>> SelectListContainsPredicate<T>(string columnName, List<int> 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 => p.Name == columnName);

      if (property == null || searchValues.Count == 0)
      {
          return Expression.Lambda<Func<T, bool>>(constant, parameter);
      }

      // Define expression :
      // x => SearchValues.Contains(x.Column)

      MemberExpression member = Expression.Property(parameter, property);

      MethodInfo method = typeof(List<int>).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<int>.Contains(int?) doesn't work.
      Expression expression = Expression.Call(constant, method, member);

      return Expression.Lambda<Func<T, bool>>(expression, parameter);
}
//此函数适用于“SelectListId int not null”列
公共静态表达式SelectListContainsPredicate(字符串列名称,列表搜索值)
{
var类型=类型(T);
ParameterExpression参数=表达式参数(类型为“x”);
恒常压力常数=表达式常数(真);
//当列无效时,返回true
PropertyInfo property=type.GetProperties().FirstOrDefault(p=>p.Name==columnName);
if(property==null | | searchValues.Count==0)
{
返回表达式.Lambda(常数,参数);
}
//定义表达式:
//x=>SearchValues.Contains(x.Column)
MemberExpression成员=Expression.Property(参数,属性);
MethodInfo method=typeof(List).GetMethod(“包含”);
常量=表达式。常量(搜索值);
//这里它抛出:
//System.ArgumentException:类型为'System.Nullable'1[System.Int32]的表达式
//无法用于方法“Boolean Contains(Int32)”的类型为“System.Int32”的参数
//因为:列是int?,而List.Contains(int?)不起作用。
Expression=Expression.Call(常量、方法、成员);
返回表达式.Lambda(表达式,参数);
}
但是我得到了一个错误,因为
SelectListId
null
的,但是
Contains
方法只有
int
参数。我能在这里做什么,知道吗

System.ArgumentException:类型为的表达式 'System.Nullable'1[System.Int32]'不能用于的参数 方法“Boolean Contains(Int32)”的类型“System.Int32”

使用

x=>(x.SelectListId!=null)?SelectedsByUser.Contains((int)(x.SelectListId)):0
并用intendent值替换0。

我在这里找到了解决方案

表达式。Convert
方法,我用了:-)

Expression memberAsInt=Expression.Convert(成员,typeof(Int32));
Expression=Expression.Call(常量、方法、memberAsInt);
返回表达式.Lambda(表达式,参数);

Hi draz,我如何在Expression.Call中执行
(int)(x.SelectListId)
?我将其固定为
(int)(x.SelectListId)
您正在查找的
mycollection.Where(x=>(x.SelectListId!=null)?selectedsByUser.Contains((int)(x.SelectListId)):false)
?更好的是:
mycollection.Where(x=>(x.SelectListId!=null)和&selectEdsByUser.Contains((int)(x.SelectListId))
Expression memberAsInt = Expression.Convert(member, typeof(Int32));

Expression expression = Expression.Call(constant, method, memberAsInt);

return Expression.Lambda<Func<T, bool>>(expression, parameter);