Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DateTime相等可为空问题的Linq表达式_C#_Entity Framework_Linq - Fatal编程技术网

C# DateTime相等可为空问题的Linq表达式

C# DateTime相等可为空问题的Linq表达式,c#,entity-framework,linq,C#,Entity Framework,Linq,我正在尝试使用以下代码段从SQL Server中搜索DateTime数据字段: DateTime toDate = DateTime.Parse("14-11-2016"); 正如您所看到的,toDate字段不是空的 var maxComparisonExpression = Expression.Equal(Expression.Call( Expression.Invoke(searchColumnName, row), ty

我正在尝试使用以下代码段从SQL Server中搜索DateTime数据字段:

DateTime toDate = DateTime.Parse("14-11-2016");
正如您所看到的,toDate字段不是空的

var maxComparisonExpression = Expression.Equal(Expression.Call(
                Expression.Invoke(searchColumnName, row),
                typeof(DateTime).GetMethod("CompareTo", new[] { typeof(DateTime) }),
                Expression.Constant(toDate, typeof(DateTime))),
                Expression.Constant(0, typeof(DateTime)));
如果此代码不正确,则在执行maxComparisonExpression时会显示以下错误

Method 'Int32 CompareTo(System.DateTime)' declared on type 'System.DateTime' cannot be called with instance of type 'System.Nullable`1[System.DateTime]'
到目前为止,我已经将typeof(DateTime)更改为typeof(DateTime?),这没有任何效果

我认为这与第二个参数有关

Expression.Constant(0, typeof(DateTime))
但我看不出如何解决这个问题。有人能给我一些解决这个问题的建议吗

更新:

我有一个用于高级搜索的页面。在这个页面上有二十多个搜索字段,每个字段都有一个选择下拉列表,提供比较运算符,如:等于、小于、大于等等

所以我写了一些表达式来处理特定的数据类型。最终将使用通用模式将其转换为库

使用以下代码定义[field]==fromInt在Linqpad中执行良好

int fromInt = 101462975;

Expression<Func<Table_Name, int>> searchColumnName = x => x.Id;

IQueryable<Table_Name> retval = Entities.Table_Name.AsQueryable();

var row = Expression.Parameter(typeof(Table_Name), "Table_Name");

var ComparisonExpression = Expression.Equal(Expression.Call(
                           Expression.Invoke(searchColumnName, row),
                           typeof(int).GetMethod("CompareTo", new[] { typeof(int) }),
                           Expression.Constant(fromInt)),
                           Expression.Constant(0, typeof(int)));
但这只是抛出了相同的错误,然后我将常数替换为空值,以给出:

var ComparisonExpression = Expression.Equal(Expression.Call(
                           Expression.Invoke(searchColumnName, row),
                           typeof(DateTime).GetMethod("CompareTo", new[] { typeof(DateTime) }),
                           Expression.Constant(toDate, typeof(DateTime))),
                           null);
虽然这不起作用,但提供的错误表明它不喜欢null值参数

我遇到了以下链接


我已经尝试了这里的建议,即需要中间操作。也不起作用。

比较的结果是一个整数,尽管您希望将结果与日期时间进行比较:

Expression.Constant(0, typeof(DateTime /* oops */))
那没有道理,是吗?与
int
比较:

Expression.Constant(0, typeof(int))
在这种情况下,您可以省略以下类型:

Expression.Constant(0)

另一个问题是
Expression.Invoke(searchColumnName,row)
返回的是
DateTime?
,而不是
DateTime
。你必须以这种或那种方式转换它。您可以添加
GetDateTimeValue
方法:

var ComparisonExpression = Expression.Equal(Expression.Call(
                           GetDateTimeValue(Expression.Invoke(searchColumnName, row)),
                           typeof(DateTime).GetMethod("CompareTo", new[] { typeof(DateTime) }),
                           Expression.Constant(toDate, typeof(DateTime))),
                           null);
其中
GetDateTimeValue
为:

private readonly static MethodInfo nullableDateTimeGetValueOrDefaultMethodInfo = typeof(DateTime?).GetMethod(nameof(Nullable<DateTime>.GetValueOrDefault), new Type[] { });

private static Expression GetDateTimeValue(Expression expr)
{
    return Expression.Call(expr, nullableDateTimeGetValueOrDefaultMethodInfo);
}
private readonly static MethodInfo nullableDateTimeGetValueOrDefaultMethodInfo=typeof(DateTime?).GetMethod(nameof(Nullable.GetValueOrDefault),新类型[]{};
私有静态表达式GetDateTimeValue(表达式expr)
{
返回表达式.Call(expr,NullableDateTimeGetValuerDefaultMethodInfo);
}

此代码段中有许多错误,修复一个错误只会让您转到下一个错误。原始异常表示
Expression.Invoke(searchColumnName,row)
的类型是
DateTime?
。你最好告诉我们你想要实现什么,并展示整个方法。
var ComparisonExpression = Expression.Equal(Expression.Call(
                           GetDateTimeValue(Expression.Invoke(searchColumnName, row)),
                           typeof(DateTime).GetMethod("CompareTo", new[] { typeof(DateTime) }),
                           Expression.Constant(toDate, typeof(DateTime))),
                           null);
private readonly static MethodInfo nullableDateTimeGetValueOrDefaultMethodInfo = typeof(DateTime?).GetMethod(nameof(Nullable<DateTime>.GetValueOrDefault), new Type[] { });

private static Expression GetDateTimeValue(Expression expr)
{
    return Expression.Call(expr, nullableDateTimeGetValueOrDefaultMethodInfo);
}