相关疑难解决方法(0)

LINQ to Entities无法识别方法'Int32 Parse(System.String)'方法,并且此方法无法转换为存储表达式

我正在使用实体框架,我有一行代码,它将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)

我得到这个错误.正如你所看到的,我已经尝试了不同的东西,没有任何工作,所以任何帮助都会很棒.

c# linq int entity int32

11
推荐指数
1
解决办法
4万
查看次数

在组连接linq查询中将字符串转换为十进制

我必须连接两个表,但只返回第二个表中的那些记录,其中与第一个表中的记录关联的所有记录的"值"的总和是相同的.

from p in db.TPs
join n in db.TNs
on p.Key equals n.Key
where (decimal.Parse(p.Value) == db.TNs.Where( nn => nn.Key == p.Key )
                                       .Sum( nn=> decimal.Parse(kk.Value)))
Run Code Online (Sandbox Code Playgroud)

我正在使用Entity Framework Code-First.

当然,Linq抱怨道

LINQ to Entities无法识别方法'System.Decimal Parse(System.String)'方法

表很大,我必须减少输出,因此无法在客户端进行此转换.列类型转换也不是一种选择.

SQL查询是:

select * from TP as p
join * from TN as n on n.Key = p.Key
where p.Value = (select sum(cast(n.Value as decimal(12,2))) from TN where Key = p.Key)
Run Code Online (Sandbox Code Playgroud)

c# sql linq entity-framework

9
推荐指数
2
解决办法
1万
查看次数

标签 统计

c# ×2

linq ×2

entity ×1

entity-framework ×1

int ×1

int32 ×1

sql ×1