强制将DateTimeOffset转换为Nullable <datetimeoffset>

时间:2018-07-24 15:59:43

标签: c# linq

我需要编写一个动态构建LINQ查询表达式的函数。我无法构建将EqualsDateTimeOffset进行比较的DateTimeOffset?表达式(Expression.Equal抱怨无法比较类型),也无法转换将DateTimeOffset转换为DateTimeOffset?以使Expression.Equal工作,因为无论我使用哪种转换策略,都无法将DateTimeOffset转换为DateTimeOffset?

MyEntity.cs:

public class MyEntity {
    public DateTimeOffset? DeliverOn;
}

Main.cs:

public void Test {
    IQueryable<MyEntity> MyEntityList = Enumerable.Empty<MyEntity>().AsQueryable();
    DateTimeOffset? dt1 = new DateTimeOffset(2018, 01, 01, 00, 00, 00, TimeSpan.Zero);
    var expr = WhereEquals(MyEntityList, t => t.DeliverOn, dt1);
}

ExpressionBuilder.cs:

public static IQueryable<TSource> WhereEquals<TSource, TValue>(IQueryable<TSource> source, Expression<Func<TSource, TValue>> selector, TValue value) {
    return source.Where(Expression.Lambda<Func<TSource, bool>>(Expression.Equal(selector.Body, Expression.Constant(value)), selector.Parameters));
}

抛出:System.InvalidOperationException: 'The binary operator Equal is not defined for the types 'System.Nullable``1[System.DateTimeOffset]' and 'System.DateTimeOffset'.'

尽管dt1在编译时是DateTimeOffset?,但在运行时,C#认为dt1DateTimeOffset。我可以解决这个问题吗?

我尝试过:

  • DateTimeOffset? dt1 = (DateTimeOffset?)new DateTimeOffset(2018, 01, 01, 00, 00, 00, TimeSpan.Zero);
  • DateTimeOffset? dt1 = (DateTimeOffset?)(object)new DateTimeOffset(2018, 01, 01, 00, 00, 00, TimeSpan.Zero);
  • DateTimeOffset? dt1 = (DateTimeOffset?)Convert.ChangeType(new DateTimeOffset(2018, 01, 01, 00, 00, 00, TimeSpan.Zero), typeof());

但是dt1仍然是DateTimeOffset

1 个答案:

答案 0 :(得分:2)

使用重载Expression.Constant(Object, Type)来指定要在比较中使用的值的类型。

未指定类型,Nullable<DateTimeOffset>被解包为简单的DateTimeOffset

void Main()
{
    //IQueryable<MyEntity> MyEntityList = Enumerable.Empty<MyEntity>().AsQueryable();
    var MyEntityList = new List<MyEntity>();
    MyEntityList.Add(new MyEntity { DeliverOn = new DateTimeOffset(2018, 01, 01, 00, 00, 00, TimeSpan.Zero) });
    DateTimeOffset? dt1 = new DateTimeOffset(2018, 01, 01, 00, 00, 00, TimeSpan.Zero);
    var expr = WhereEquals<MyEntity, DateTimeOffset?>(MyEntityList.AsQueryable(), t => t.DeliverOn, (DateTimeOffset?)dt1);
    Console.WriteLine($"{expr.Count()} item(s) found");

    // Output:
    // selector.GetType() is System.Linq.Expressions.Expression`1[System.Func`2[UserQuery + MyEntity, System.Nullable`1[System.DateTimeOffset]]]
    // typeof(TValue) is System.Nullable`1[System.DateTimeOffset]
    // value.GetType() is System.DateTimeOffset
    // 1 item(s) found
}

public class MyEntity
{
    public DateTimeOffset? DeliverOn;
}

public static IQueryable<TSource> WhereEquals<TSource, TValue>(IQueryable<TSource> source, Expression<Func<TSource, TValue>> selector, TValue value)
{
    Console.WriteLine($"selector.GetType() is {selector.GetType()}");
    Console.WriteLine($"typeof(TValue) is {typeof(TValue)}");
    Console.WriteLine($"value.GetType() is {value.GetType()}");
    return source.Where(Expression.Lambda<Func<TSource, bool>>(Expression.Equal(selector.Body, Expression.Constant(value, typeof(TValue))), selector.Parameters));
}

相关,尽管接受的答案使用Expression.Convert(),我认为这是不必要的。

Working with nullable types in Expression Trees