我想在Linq中創建一個查詢到實體。我希望它返回包含從我查詢的表中的字符串派生的DateTime屬性的對象。數據(在SQL Server中)有一個字符串日期字段(在數據庫中顯示爲VARCHAR(8)),稱爲date_occurred。它有一個名爲time_occurred的字符串時間字段(varchar(6))。如何在Linq查詢中創建日期時間值?
date_occurred的內容示例是2013年10月7日的「20131007」。time_occurred的內容示例是「145710」,表示14:57之後10秒。
我已經嘗試了兩種方法不起作用:
Dim ATQuery = From e In EntityContext.vwSecAppAuditToday
Order By e.date_occurred, e.time_occurred
Select New AuditEntry With {
.EventTime = DateTime.ParseExact(Trim(e.date_occurred) & Trim(e.time_occurred), "yyyyMMddHHmmss", CultureInfo.InvariantCulture),
.ServerName = e.server_name
}
這將引發包含一條消息,指出一個NotSupportedException
:「LINQ到實體無法識別方法「的System.DateTime ParseExact(System.String, System.String,System.IFormatProvider)'方法,並且此方法不能轉換爲存儲表達式。「
在這之前,我想:
Dim ATQuery = From e In EntityContext.vwSecAppAuditToday
Order By e.date_occurred, e.time_occurred
Select New AuditEntry With {
.EventTime = New DateTime(Integer.Parse(e.date_occurred.Substring(0, 4)),
Integer.Parse(e.date_occurred.Substring(4, 2)),
Integer.Parse(e.date_occurred.Substring(6, 2)),
Integer.Parse(e.time_occurred.Substring(0, 2)),
Integer.Parse(e.time_occurred.Substring(2, 2)),
Integer.Parse(e.time_occurred.Substring(4, 2))),
.ServerName = e.server_name
}
這也將引發NotSupportedException
。在這種情況下,消息指出:「只有無參數的構造函數和初始化器在LINQ to Entities中受支持。」
正在嘗試使用Linq來實體嗎?
編輯:評論警報
對於那些誰看到這篇文章後,莫霍面和馬特·約翰遜取得了非常有用的意見。我用+1標記了這些。
你能向我們展示conten 「e.date_occured」和「e.time_occured」? – AlexB
優秀的建議。我將編輯帖子。謝謝! – Tim
您正在使用哪個數據庫,以及您正在使用哪些數據庫類型? –