简体   繁体   English

Linq无法转换为ToShortDateString()

[英]Linq Cannot Translate ToShortDateString()

Can you please tell me how to fix the query below. 您能否告诉我如何解决以下查询。 LINQ was formating my dates as written out example: August 8th 2015. I need 8/8/2015. LINQ正在格式化我的日期,如写出的示例:2015年8月8日。我需要2015年8月8日。 Every attempt i try gives me a different error message. 我尝试的每一次尝试都给我一个不同的错误消息。 What am i doing wrong. 我究竟做错了什么。

var orgData = (from d in db.tbl_FormsSubmittedValues
               where d.SiteID == _siteID
               & d.FormID == _formID
               & ((d.tbl_FormsSubmitted.UserID == _userID && _viewOwnOnly) || !_viewOwnOnly)
               & ((_search != "" && d.Value.Contains(_search)) || _search == "")
               select new
               {
                   d.FormSubmissionID,
                   Value = d.ValueDate.HasValue ? d.ValueDate.Value.ToShortDateString(): d.ValueLong != null ? d.ValueLong : d.Value,
                   d.FieldID,
                   d.FormID,
                   d.SiteID,
                   d.tbl_FormsSubmitted,
                   d.tbl_FormsSubmitted.UserID,
               }).ToList();

Original Line That was giving me written out date instead of mm/dd/yyyy 原始行给我写出日期而不是mm / dd / yyyy

 Value = d.ValueDate != null ? d.ValueDate.ToString() : d.ValueLong != null ? d.ValueLong : d.Value,

Problem Line: 问题专线:

Value = d.ValueDate.HasValue ? d.ValueDate.Value.ToShortDateString(): d.ValueLong != null ? d.ValueLong : d.Value,

Error: 错误:

Could not translate expression 'd.ValueDate.Value.ToShortDateString()' into SQL and could not treat it as a local expression.

An alternative to creating a new anonymous object would be to create a new typed object (eg select new Submission() { ... } ) that exposes a read-only property for rendering a short date. 创建新的匿名对象的替代方法是创建一个新的类型化对象(例如, select new Submission() { ... } ),该对象公开用于渲染短日期的只读属性。 This may add some overhead for such a simple operation, but it offers some flexibility (you can format more fields later) and performance (any additional query or filter operations are performed against the database instead of in memory). 这样的简单操作可能会增加一些开销,但它提供了一定的灵活性(以后可以格式化更多字段)和性能(对数据库而不是在内存中执行任何其他查询或筛选操作)。

Or, you could push the work down the pipe to the presentation layer. 或者,您可以将工作推到显示层。 Let the code responsible for displaying the data to the user handle converting or displaying the short date and keep your data/business layers lean and open to other interpretations. 让负责向用户显示数据的代码处理转换或显示短日期,并使您的数据/业务层保持精简并接受其他解释。 This would keep you from having to go back and change the query or modify your POCO to accommodate a change in the preference for date format. 这将使您不必再回去更改查询或修改POCO以适应日期格式首选项的更改。 For example, what if you had another person who preferred the date to be shown as YYYY-dd-MM? 例如,如果您有另一个人希望将日期显示为YYYY-dd-MM,该怎么办?

Do the formatting outside of the actual sql. 在实际sql之外进行格式化。 Something like: 就像是:

orgData = (from d in db.tbl_FormsSubmittedValues
           where d.SiteID == _siteID
           & d.FormID == _formID
           & ((d.tbl_FormsSubmitted.UserID == _userID && _viewOwnOnly) || !_viewOwnOnly)
           & ((_search != "" && d.Value.Contains(_search)) || _search == "")
           select new {
                       d.FormSubmissionID,
                       Value = d.ValueDate.HasValue ? d.ValueDate.Value: d.ValueLong != null ? d.ValueLong : d.Value,
                       d.FieldID,
                       d.FormID,
                       d.SiteID,
                       d.tbl_FormsSubmitted,
                       d.tbl_FormsSubmitted.UserID
                      }).AsEnumerable() //Using this to get the query to run.
           //Anything after this should not be done on the database side.
           .Select(d => new
           {
               d.FormSubmissionID,
               Value = d.ValueDate.HasValue ? FormatDate(d.ValueDate.Value): d.ValueLong != null ? d.ValueLong : d.Value,
               d.FieldID,
               d.FormID,
               d.SiteID,
               d.tbl_FormsSubmitted,
               d.tbl_FormsSubmitted.UserID,
           }).ToList();


static string FormatDate(DateTime date)
{
    return  date.Value.ToShortDateString();
}

This keeps the formatting out of the actual generated sql. 这样可以使格式化不影响实际生成的sql。

I will recommend: 我会建议:

Value = d.ValueDate.HasValue ? (d.ValueDate.Value.Day + "/" + d.ValueDate.Value.Month + "/" + d.ValueDate.Value.Year) : (d.ValueLong != null ? d.ValueLong : d.Value),

Or doing the toshortstring when displaying the data (If you are using it on a website) 或在显示数据时执行toshortstring(如果在网站上使用它)

Or even parse the date through javascript using the parsed date (json) 甚至使用解析日期(json)通过javascript解析日期

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

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