![](/img/trans.png)
[英]LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression
[英]LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression
我正在使用實體框架,並且有一行代碼將 var 轉換回數據庫的 iint。
var record = context.enrollments.SingleOrDefault
(row => row.userId == int.Parse(UserID) && row.classId == int.Parse(ClassID));
每當我嘗試運行它時,我都會收到 rhis 錯誤。 “LINQ to Entities 無法識別方法 'Int32 Parse(System.String)' 方法,並且此方法無法轉換為存儲表達式。”
我也試過這個
var record = context.enrollments.FirstOrDefault
(row => row.userId == Convert.ToInt32(UserID)
&& row.classId == Convert.ToInt32(ClassID));
我收到的只是這個錯誤消息,“LINQ to Entities 無法識別方法 'Int32 ToInt32(System.String)' 方法,並且此方法無法轉換為存儲表達式
最后我也試過這個,我知道這是不尋常的,但它在過去的類似情況下也有效。
var record = context.enrollments.SingleOrDefault
(row => row.userId == CommonDLL.Sanitize<int>.ConvertType(UserID)
&& row.classId == CommonDLL.Sanitize<int>.ConvertType(ClassID));
我得到這個錯誤。 如您所見,我嘗試了幾種不同的方法,但沒有任何效果,因此任何幫助都會很棒。
在Linq to Entity
,您應該使用您的provider
支持的query
的方法將它們轉換為expression tree
以在您的Data Base
端運行。
默認情況下,所有提供程序都必須支持一些稱為Canonical Functions
( 在此處閱讀更多內容),您還可以將user defined function
和stored procedure
user defined function
為edm functions
以在linq query
( 在此處閱讀更多內容)和( 在此處閱讀更多內容)。
此外,您可以使用提供者支持的方法,並且可以轉換為EntityFunctions和SqlFunctions 中的expression tree
。
最后關於您的問題,您可以在查詢之前轉換UserID
和ClassID
,如下所示:
var UID = int.Parse(UserID);
var CID = int.Parse(ClassID);
var record = context.enrollments.SingleOrDefault
(row => row.userId == UID && row.classId == CID);
這對我有用:
var iId = int.Parse(TextBox1.Text);
var item = db.Items.Where(p => p.ItemID == iId ).FirstOrDefault();
要完成這項工作,您應該在 lambda 表達式之外轉換您的文本框值。
您可以使用AsEnumerable()
如下:
var record = context.enrollments.AsEnumerable().FirstOrDefault(row => row.userId == Convert.ToInt32(UserID) && row.classId == Convert.ToInt32(ClassID));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.