我正在開發一個WCF RESTful服務與C#,.NET Framework 4.0和實體框架代碼第一。作爲一個字符串比較DateTime作爲一個字符串到實體不識別DateTime.parse(字符串)
我有這個類(表示在數據庫中的表):
[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");
,但我得到了以下錯誤:
{System.NotSupportedException: LINQ to Entities doesn't recognize the method 'System.DateTime Parse(System.String)', which can not be converted into an expression of the repository.
我需要那PostLine.DateUtc
是一個字符串becau我將在我的Web服務上使用它,並將它作爲JSON發送,所以它更適合我以字符串形式存儲它。
如果我使用DateTime
類型,我會得到這樣的事情在JSON響應:
{
"DateUtc": "/Date(1380924000000+0200)/",
"Description": "post_1",
"UserId": 1
}
你知道我怎麼可以在LINQ表達式比較字符串與一個DateTime?
爲什麼'DateUtc'不是'DateTime'?你的JSON序列化程序應該處理這個解析。 –
是的,但是如何在iOS和Android上分析'「/ Date(1380924000000 + 0200)/」'? – VansFannel
相關鏈接:http://stackoverflow.com/questions/10286204/the-right-json-date-format所以我可以同情這裏使用的字符串屬性。 – hvd