【发布时间】:2013-06-05 10:10:28
【问题描述】:
我遇到以下异常。我检查了设计器,类和机会代码是一个 int。
LINQ to Entities 无法识别方法 'Int32 ToInt32(System.Object)' 方法,并且该方法无法转换为存储表达式
public tblOpportunity GetOpportunityByCode(string clientCode, string opportunityCode)
{
tblOpportunity opportunity = null;
ConnectionHandler.Invoke<EntityConnection>((connection) =>
{
var context = new xxEntities();
opportunity = context.tblOpportunities.FirstOrDefault<tblOpportunity>(o => o.ClientCode == clientCode && o.OpportunityCode == Convert.ToInt32(opportunityCode));
});
return opportunity;
}
}
public partial class tblOpportunity
{
public int OpportunityCode { get; set; }
【问题讨论】:
-
你试过
o.OpportunityCode.ToString() == opportunityCode吗? -
你确定它是 int 类型,因为它可以是 int 吗? (nullable int) 或者您可以使用 automapper 将 linq 查询的输出映射到您的领域特定模型,这将减少手动映射的痛苦
-
使用这个->
o.OpportunityCode == (int)opportunityCode而不是o.OpportunityCode == Convert.ToInt32(opportunityCode)
标签: c# linq entity-framework