我有一个像这样的表达式:
var values = Enumerable.Range(1,2);
return message => message.Properties.Any(
p => p.Key == name
&& int.Parse(p.Value) >= values[0]
&& int.Parse(p.Value) <= values[1]);
Run Code Online (Sandbox Code Playgroud)
这编译得很好但是当它命中数据库时会抛出异常 'LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression '
如果我不进行解析并且有值,那么string[]我就不能在字符串上使用>=和<=运算符.
p.Value 是一个包含各种值的字符串,但在这种情况下它是 int
有没有办法可以查询数据库来执行这种语句之间的操作?
我正在使用实体框架,我有一行代码,它将var转换回数据库的iint.
var record = context.enrollments.SingleOrDefault
(row => row.userId == int.Parse(UserID) && row.classId == int.Parse(ClassID));
Run Code Online (Sandbox Code Playgroud)
每当我尝试运行它时,我都会收到此错误."LINQ to Entities无法识别方法'Int32 Parse(System.String)'方法,并且此方法无法转换为商店表达式."
我也尝试过这个
var record = context.enrollments.FirstOrDefault
(row => row.userId == Convert.ToInt32(UserID)
&& row.classId == Convert.ToInt32(ClassID));
Run Code Online (Sandbox Code Playgroud)
而我收到的只是这个错误信息,"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));
Run Code Online (Sandbox Code Playgroud)
我得到这个错误.正如你所看到的,我已经尝试了不同的东西,没有任何工作,所以任何帮助都会很棒.
我在使用LINQ概念时遇到了问题.下面我比较两个数据表,一个是数据库,另一个是excel文件.
问题是当我在两个"tempdt"之间应用连接时包含提升铸造对象错误的双值.我无法将返回类型从字符串更改为double,因为它将来可能是任何数据类型,因为它目前是双倍的,将来可能是字母数字.
var commonRows = from r1 in dt.AsEnumerable()
join r2 in tempdt.AsEnumerable()
on r1.Field<string>(0) equals r2.Field<string>(4)
select r2;
if (commonRows.Any())
{
abcdefgh = commonRows.Count();
dt123 = commonRows.CopyToDataTable();
// ring the ghanta of gutlu
}
Run Code Online (Sandbox Code Playgroud)
例外:无法将类型为"System.Double"的对象强制转换为"System.String".