【发布时间】:2026-02-01 05:30:01
【问题描述】:
我有一个 WCF 方法,用于在两个表之间进行连接。现在我遇到了这个错误:
LINQ to Entities 无法识别方法“Int32 Parse(System.String)”方法,并且该方法无法转换为存储表达式。
当我查看断点时,我在操作合同中创建的变量显示 undefined 在运行编码时。
运营合同
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getRepByCompA/{CompanyID}")]
List<Represetative> GetRepByCompA(string CompanyID);
服务方式
public List<Represetative> GetRepByCompA(string CompanyID)
{
try
{
TruckDb db = new TruckDb();
List<Represetative> RepList = new List<Represetative>();
var join = from t in db.Companies
join p in db.Represetatives on t.Id equals p.CompanyId
where t.Id == int.Parse(CompanyID) *//<-Shows CompanyID is Undefined-*
select new { t, p };
foreach (var item in join)
{
Represetative ph = new Represetative();
//REPRESETATIVES
ph.Name = item.p.Name;
ph.Email = item.p.Email;
ph.ContactNumber = item.p.ContactNumber;
ph.Quotes = item.p.Quotes;
ph.CompanyId = item.p.Id;
//REPRESETATIVES
//COMPANY
ph.Id = item.t.Id;
//COMPANY
RepList.Add(ph);
}
return RepList;
}
catch (Exception)
{
throw;
}
}
当我尝试将 CompanyID 更改为 int 时,我在运行服务时收到此错误:
合约“ITruckService”中的操作“GetRepByCompA”有一个名为“CompanyID”的路径变量,它没有“字符串”类型。 ÿ UriTemplate 路径段的变量必须具有“字符串”类型。
【问题讨论】:
-
您不能将 CompanyID 作为整数而不是字符串传递吗?
-
不,这就是我在底部提到的它说它需要是一个字符串值? @dimitarie
-
从外部将 id 转换为
int:var id = int.Parse(CompanyID); var join = from ... where where t.Id == id ...; -
它给了我这个错误:
Input string was not in a correct format.@Dennis -
@RGdent:我想,
CompanyId不包含整数。你能在解析之前在调试器中查看CompanyId吗?
标签: c# .net entity-framework linq wcf