gpt4 book ai didi

c# - 如何在 linq lambda 表达式中使用 ToShortDateString?

转载 作者:太空狗 更新时间:2023-10-29 17:32:42 26 4
gpt4 key购买 nike

我需要在使用 lambda 表达式的 linq 查询中调用 ToShortDateString:

toRet.Notification = Repositories
.portalDb.portal_notifications.OrderByDescending(p => p.id)
.FirstOrDefault(p => p.date.ToShortDateString() == shortDateString);

但是我得到了错误:

An exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll but was not handled in user code

Additional information: LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method, and this method cannot be translated into a store expression.

考虑到我确实需要使用 ToShortDateString(),我该怎么办?

谢谢。

最佳答案

Linq to Entities 无法将 ToSortDateString 方法转换为 SQL 代码。你不能在服务器端调用它。要么将过滤移动到客户端(这会将所有数据从服务器传输到客户端),要么考虑使用 server-side functions将日期作为日期的一部分(您应该传递 DateTime 对象而不是 shortDateString):

EntityFunctions.TruncateTime(p.date) == dateWithoutTime

关于c# - 如何在 linq lambda 表达式中使用 ToShortDateString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17655305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com