比较日期时间为一个字符串转换为LINQ到实体不承认DateTime.parse(串) [英] Compare DateTime as a string into a LINQ to Entities doesn't recognize DateTime.parse(string)

查看:514
本文介绍了比较日期时间为一个字符串转换为LINQ到实体不承认DateTime.parse(串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个WCF RESTful服务使用C#,.NET框架4.0和实体框架code首先

I'm developing a WCF RESTful service with C#, .NET Framework 4.0 and Entity Framework Code First.

我有这个类(即重新presents表数据库):

I have this class (that represents a table on database):

[DataContract]
public class PostLine
{
    public int PostLineId { get; set; }

    [DataMember]
    public int? UserId { get; set; }

    [DataMember]
    public string Description { get; set; }

    [DataMember]
    public string DateUtc { get; set; }

    public User Author { get; set; }
}

和我试图做到这一点:

DateTime fourDaysAgo = DateTime.Now.Date.AddDays(-4);

var postLines = 
    context.PostLines.Where(p => DateTime.Compare(DateTime.Parse(p.DateUtc), fourDaysAgo) > 0).Include("Author");

不过,我得到以下错误:

But I get the following error:

{System.NotSupportedException:LINQ到实体不能识别方法的System.DateTime解析(System.String),它不能转换成库的前pression

我需要 PostLine.DateUtc 是一个字符串,因为我会用我的网络服务,并将其发送为JSON,因此它更适合我将其存储为一个字符串。

I need that PostLine.DateUtc to be a string because I'm going to use it on my web service, and send it as JSON, so its better for me to store it as a string.

如果我用的DateTime 的类型,我会得到像这样的JSON响应:

If I use DateTime type, I'm going to get something like this on JSON response:

{
    "DateUtc": "/Date(1380924000000+0200)/",
    "Description": "post_1",
    "UserId": 1
}

你知道我怎么能比较字符串与一个LINQ EX pression一个DateTime?

推荐答案

我认为最好的办法将是分割财产一分为二。

I think the best approach will be to split the property in two.

实体框架想要一个的DateTime 属性。这是非常合情合理的。

Entity Framework wants a DateTime property. That makes perfect sense.

有关系列化你想有一个字符串属性。这也非常有意义。

For serialization you want a string property. That also makes perfect sense.

不过,你要使用这两个一个单一的财产,那是没有意义的,而且是没有必要的。

However, you're trying to use a single property for both, and that doesn't make sense, and isn't necessary.

[DataContract]
public class PostLine
{
    ...

    public DateTime DateUtcAsDateTime { get; set; }

    [DataMember, NotMapped]
    public string DateUtcAsString {
        get { return DateUtcAsDateTime.ToString(); }
        set { DateUtcAsDateTime = DateTime.Parse(value); }
    }

    ...
}

现在, DateUtcAsDateTime 将被用于实体框架,而 DateUtcAsString 将被实体框架忽略,因为它有一个 NotMapped 属性。

Now, DateUtcAsDateTime will be used by Entity Framework, and DateUtcAsString will be ignored by Entity Framework as it has a NotMapped attribute.

DateUtcAsString ,在另一方面,是这些属性,有一个数据成员属性只有一个,所以应该是被序列化的唯一的一个。

DateUtcAsString, on the other hand, is the only one of these properties that has a DataMember attribute, so should be the only one that gets serialized.

当然,你可以重命名这些属性之一回 DateUtc ,如果你想要的。

You can of course rename one of these properties back to DateUtc, if you want.

更新:马特·约翰逊指出,改进将是指定格式的方式,都会导致完全相同的字符串。这样可以确保您的字符串不改变,只是因为你的code被移动到恰好有不同的区域设置另一台服务器。

Update: as Matt Johnson points out, an improvement would be to specify the format in a way that always results in the exact same string. This ensures your strings don't change, just because your code gets moved to another server that happens to have different regional settings.

[DataMember, NotMapped]
public string DateUtcAsString {
    get { return DateUtcAsDateTime.ToString("o"); }
    set { DateUtcAsDateTime = DateTime.Parse(value, "o", null, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); }
}

请注意,我用 DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal 而不是 DateTimeStyles.RoundTripKind 他建议,因为这个名字 DateUtc 强烈建议你的总是的要UTC,从未本地时间。而且我不指定任何明确的文化,因为O格式已经是独立的文化。

Note that I am using DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal instead of the DateTimeStyles.RoundTripKind that he suggested, because the name DateUtc strongly suggests you always want UTC, never local time. And I'm not specifying any culture explicitly, as the "o" format is already independent of culture.

您可以,如果是更容易为你的另一code来处理,使用R而不是O有同样的好处。

You could, if it's easier for your other code to handle, use "r" instead of "o" with the same benefits.

这篇关于比较日期时间为一个字符串转换为LINQ到实体不承认DateTime.parse(串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆