Linq 表达式不适用于可空字符串字段
Linq Expression is not working for Nullable string field
我正在尝试创建 LINQ 表达式。下面的代码 2 部分适用于 NOT NULL 字符串字段,但对于 NULL 字符串字段,最终表达式显示“System.NullReferenceException:'对象引用未设置为对象的实例。”当执行 lamda.compile()
//code for Contains
Expression expressionSub = null;
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string), typeof(StringComparison) });
var comparisonValue = Expression.Constant(StringComparison.OrdinalIgnoreCase, typeof(StringComparison));
var actualValue = Expression.Constant(propertyValue, typeof(string));
expressionSub = Expression.Call(propertyName, method, actualValue, comparisonValue);
return expressionSub;
//code for Equal and not equal
MethodInfo method = typeof(String).GetMethod("ToLower", Type.EmptyTypes);
var dynamicExpression = Expression.Call(propertyName, method);
Expression constExp = Expression.Constant(propertyValue.ToString().ToLower());
if (operatorName == "equal")
return Expression.Equal(dynamicExpression, constExp);
else if (operatorName == "notequal")
return Expression.NotEqual(dynamicExpression, constExp);
else
return null;
如何使以上 2 段代码适用于可空字段(例如,我的电子邮件列在 SQL 中可以为空,名称列不为空。下面的代码适用于名称但不适用于电子邮件)
您可以在此处使用 Expression.Coalesce
,这样它将用 "" 替换可能的空值,这样字符串函数就不会失败。
下面是修复相同问题的类似代码:
string filterValue = "sa";
var filterValueExpression = Expression.Constant(filterValue);
var actualValueParameterExpression = Expression.Parameter(typeof(string));
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string), typeof(StringComparison) });
var comparisonExpression = Expression.Constant(StringComparison.OrdinalIgnoreCase, typeof(StringComparison));
//here we replace possible null with ""
var coallesceExpression = Expression.Coalesce(actualValueParameterExpression, Expression.Constant(""));
var expressionSub = Expression.Call(coallesceExpression, method, filterValueExpression, comparisonExpression);
var lambda = Expression.Lambda<Func<string, bool>>(expressionSub, actualValueParameterExpression);
var func = lambda.Compile();
string actualValue = null;
var result = func(actualValue);
我正在尝试创建 LINQ 表达式。下面的代码 2 部分适用于 NOT NULL 字符串字段,但对于 NULL 字符串字段,最终表达式显示“System.NullReferenceException:'对象引用未设置为对象的实例。”当执行 lamda.compile()
//code for Contains
Expression expressionSub = null;
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string), typeof(StringComparison) });
var comparisonValue = Expression.Constant(StringComparison.OrdinalIgnoreCase, typeof(StringComparison));
var actualValue = Expression.Constant(propertyValue, typeof(string));
expressionSub = Expression.Call(propertyName, method, actualValue, comparisonValue);
return expressionSub;
//code for Equal and not equal
MethodInfo method = typeof(String).GetMethod("ToLower", Type.EmptyTypes);
var dynamicExpression = Expression.Call(propertyName, method);
Expression constExp = Expression.Constant(propertyValue.ToString().ToLower());
if (operatorName == "equal")
return Expression.Equal(dynamicExpression, constExp);
else if (operatorName == "notequal")
return Expression.NotEqual(dynamicExpression, constExp);
else
return null;
如何使以上 2 段代码适用于可空字段(例如,我的电子邮件列在 SQL 中可以为空,名称列不为空。下面的代码适用于名称但不适用于电子邮件)
您可以在此处使用 Expression.Coalesce
,这样它将用 "" 替换可能的空值,这样字符串函数就不会失败。
下面是修复相同问题的类似代码:
string filterValue = "sa";
var filterValueExpression = Expression.Constant(filterValue);
var actualValueParameterExpression = Expression.Parameter(typeof(string));
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string), typeof(StringComparison) });
var comparisonExpression = Expression.Constant(StringComparison.OrdinalIgnoreCase, typeof(StringComparison));
//here we replace possible null with ""
var coallesceExpression = Expression.Coalesce(actualValueParameterExpression, Expression.Constant(""));
var expressionSub = Expression.Call(coallesceExpression, method, filterValueExpression, comparisonExpression);
var lambda = Expression.Lambda<Func<string, bool>>(expressionSub, actualValueParameterExpression);
var func = lambda.Compile();
string actualValue = null;
var result = func(actualValue);