![](/img/trans.png)
[英]LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression
[英]Linq to entities Expression parse string to int
我在實體表達式的linq中使用的此代碼有問題:
int _val = Convert.ToInt32(value);
Expression term1 = Expression.Equal(Expression.Property(Expression.PropertyOrField(bookProperty, "type"), "key"), Expression.Constant("taille", typeof(string)));
Expression temp1 = Expression.Property(bookProperty, "value");
MethodInfo Parse = typeof(int).GetMethod("Parse", new[] { typeof(string) });
Expression term2 = Expression.GreaterThanOrEqual(Expression.Call(Parse, temp1), Expression.Constant(_val, typeof(int)));
conditions = Expression.And(term1, term2);
bookproperty是一個為其他對象(Book)設置屬性的對象,該屬性的值存儲在數據庫的“ value”字段中。 因為value可以存儲布爾值,整數,雙精度數,文本等,所以將其設置為字符串。
在這種情況下,我需要將字符串解析為int。
我認為這是行不通的,因為某些值字段存儲了不可解析的值。
我得到這個錯誤:
FormatException:輸入字符串的格式不正確。
但是我不知道該如何處理。
謝謝你的幫助。
最后,我發現解決方案只是操作員的問題:
該表達式的目標是獲得一個如下的linq查詢:
bookProperty.Where(b=> b.type== "taille && b.value == 1)
為此,我使用了Expression.And()
運算符。 那是錯誤: &&
對應噸AndAlso
運算符
我認為如果該值不是int,則應該停止該操作。 您可以使用int.TryParse方法
if (!int.TryParse(bookProperty, out bookProperty))
{
// break the operation. You can also have error handling code here.
return;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.