EntityFunctions.TruncateTime and unit tests

后端 未结 6 1608
悲&欢浪女
悲&欢浪女 2020-12-14 15:57

I\'m using System.Data.Objects.EntityFunctions.TruncateTime method to get date part of a datetime in my query:

if (searchOptions.Date.HasValue)
         


        
6条回答
  •  没有蜡笔的小新
    2020-12-14 16:30

    You can define a new static function (you can have it as an extension method if you want):

        [EdmFunction("Edm", "TruncateTime")]
        public static DateTime? TruncateTime(DateTime? date)
        {
            return date.HasValue ? date.Value.Date : (DateTime?)null;
        }
    

    Then you can then use that function in LINQ to Entities and LINQ to Objects and it will work. However, that method means that you would have to replace calls to EntityFunctions with calls to your new class.

    Another, better (but more involved) option would be to use an expression visitor, and write a custom provider for your in-memory DbSets to replace the calls to EntityFunctions with calls to in-memory implementations.

提交回复
热议问题