【发布时间】:2020-10-30 14:06:45
【问题描述】:
我正在尝试比较 Date、Hours、Minutes、Seconds 中的两个日期,但是当我调用时 x.CreationTime.TimeOfDay 出现了可为空的异常。
我认为这个link 的答案会解决我的问题,但是在我找到解决方案之后,问题仍然存在 这是我的查询:
public async Task<List<MessageDto>> getMessageHistory(long userId, string code, long HCSId, long Role,DateTime latestMessageDateTime , DateTime messageDateBeforeSeeMore)
{
var result =await _repository.GetAllIncluding(x => x.listOfMessages)
.Where(x => ((x.receiverID == userId|| x.CreatorUserId == userId)
&& x.code== code) && (
x.CreationTime != null
&& x.CreationTime.Date > latestMessageDateTime.Date
&& x.CreationTime.TimeOfDay !=null
&& x.CreationTime.TimeOfDay.Hours > latestMessageDateTime.TimeOfDay.Hours /*<===== this cause the problem if I remove it the query working fine*/
)
&& x.listOfMessages.Any(x => x.HCSId== HCSId)
).OrderBy(message => message.CreationTime).ToListAsync();
return result ;
}
更新:
异常详情:
- $exception {"Object reference not set to an instance of an object."} System.NullReferenceException
+ Data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
at MyProject.ChatAppService.MessageAppService.<getMessageHistory>d__19.MoveNext() in D:\WorkSpace\MyProject\aspnet-core\src\MyProject.Application\ChatAppService\MessageAppService.cs:line 346
+ TargetSite {Void MoveNext()} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
更新 2: 我遵循@RandRandom 提到的步骤
我发现的低于异常
The LINQ expression 'DbSet<Message>
.Where(m => m.receiverID == __citizenId_0 || m.CreatorUserId == __userId_0 && m.code == __code_1 && DbSet<HCSMessages>
.Where(h => EF.Property<Nullable<long>>(m, "Id") != null && EF.Property<Nullable<long>>(m, "Id") == EF.Property<Nullable<long>>(h, "messageId"))
.Any(h => h.HCsId == __HCSId_2) && m.CreationTime > __messageDateBeforeSeeMore_3)
.Where(m => m.CreationTime.Date > __latestMessageDateTime_Date_4)
.Where(m => m.CreationTime.TimeOfDay.Hours > __latestMessageDateTime_TimeOfDay_Hours_5)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
【问题讨论】:
-
我有点怀疑你说的是否正确,因为
DateTime和TimOfDay都是结构,不能为空,所以你不应该看到NullReferenceException。如果其中一个声明为Nullable<DateTime>或DateTime?,您可以获得NullReferenceException,但事实并非如此。您能提供异常、消息和堆栈跟踪的完整详细信息吗? -
@RandRandom 我正在尝试提到的解决方案,我想为您提供额外的详细信息!
-
@RandRandom 您现在可以查看异常详情。
-
更新了我的答案,抱歉回复晚了,放假了。
标签: c# datetime .net-core aspnetboilerplate ef-core-3.1