简体   繁体   English

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

[英]How to use ToShortDateString in linq lambda expression?

I need to call ToShortDateString in a linq query suing lambda expressions: 我需要使用lambda表达式在linq查询中调用ToShortDateString:

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

but I get the error: 但是我得到了错误:

An exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll but was not handled in user code System.Data.Entity.dll中发生类型为'System.NotSupportedException'的异常,但未在用户代码中处理

Additional information: LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method, and this method cannot be translated into a store expression. 附加信息:LINQ to Entities无法识别方法'System.String ToShortDateString()',并且该方法无法转换为商店表达式。

What can I do, considering that I do need to use ToShortDateString() ? 考虑到我确实需要使用ToShortDateString() ,该怎么办?

Thanks. 谢谢。

Linq to Entities cannot convert ToSortDateString method into SQL code. Linq to Entities无法将ToSortDateString方法转换为SQL代码。 You can't call it on server side. 您不能在服务器端调用它。 Either move filtering to client side (that will transfer all data from server to client), or consider to use server-side functions to take date part of date (you should pass DateTime object instead of shortDateString ): 要么将过滤移到客户端(它将所有数据从服务器传输到客户端),要么考虑使用服务器端函数将日期作为日期的一部分(您应该传递DateTime对象而不是shortDateString ):

EntityFunctions.TruncateTime(p.date) == dateWithoutTime

You shouldn't be forcing a string comparison when what you're working with is Date/time data - as soon as you force string comparisons, you're suddenly having to deal with how the strings are formatted. 当您使用的是日期/时间数据时,您不应该强制进行字符串比较-强制进行字符串比较后,您突然不得不处理如何格式化字符串。

Instead, have something like: 相反,请执行以下操作:

var endDate = targetDate.AddDays(1);

toRet.Notification = Repositories
.portalDb.portal_notifications.OrderByDescending(p =>       p.id)
.FirstOrDefault(p => p.date >= targetDate && p.date < endDate);

(Assuming that targetDate is whatever DateTime variable you had that was used to produce shortDateString in your code, and is already a DateTime with no time value) (假设targetDate是您在代码中用于生成shortDateString任何DateTime变量,并且已经是没有时间值的DateTime了)

Try this, 尝试这个,

You can also used with below code. 您也可以使用以下代码。

Activity = String.Format("{0} {1}", String.Format("{0:dd-MMM-yyyy}", s.SLIDESHEETDATE), String.Format("{0:HH:mm}", s.ENDDATETIME))

ToShortDateString() method usually used to work only with date and ignore time stamps. ToShortDateString()方法通常仅用于日期,而忽略时间戳。

You will get exactly today result-set by using the following query. 通过使用以下查询,您将确切地获得今天的结果集。

Repositories.portalDb.portal_notifications.OrderByDescending(p =>       p.id)
        .FirstOrDefault(p => p.date.Date == DateTime.Now.Date);

By using Date property of DateTime struct you can just fetch record of that date only. 通过使用DateTime结构的Date属性,您仅可以获取该日期的记录。

Note: Linq to Objects. 注意:Linq to对象。 Only works if you CAN (or have option) to bypass ToShortDateString() method 仅在您可以(或有选择)绕过ToShortDateString()方法时有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM